Abstract class in java

 program to illustrate the concept of abstract class

package com.company;
abstract class base2{// you must use abstract keyword to create an abstract class
public base2(){
System.out.println("im constructor");
}
public void fun(){
System.out.println("hy");
}
abstract public void greet();// this method which must be initialize in chlid classes
}
class derived2 extends base2{
public void greet(){
System.out.println("heloo");
}
}
abstract class derived3 extends base2{ //if you don't want to create abstract method
public void fun1(){ //you have to make child class abstract
System.out.println("hy world");
}
}
public class abstractClass {
public static void main(String[] args) {
derived2 d=new derived2();
d.greet();
d.fun();
//base2 b= new base2(); your cannot define object for this
/* derived3 r= new derived3();
r.fun1(); you cannot call this since it is inside abstract class */
}
}

 








No comments:

Post a Comment

    Hello, I'm Vishal Rana  I'm 19 years old  I live in Himachal Pradesh and  currently I'm studying in college for my UG degree...