Techiee Techiee - 16 days ago 6
Java Question

Can we instantiate an abstract class? Is it possible?

I read in SCJP6 that an abstract class cannot be instantiated. But read somewhere that they can be instantiated. Is is true? I am new to Java and would like if anyone could explain this in detail. An example would also be a good.

Answer

You can create a reference of an abstract class, but cant instantiate it. For eg.

public abstract class AbstractClass {

    public abstract void abstractMethod();
    public  void concreteMethod(){
        System.out.println("am in concreteMethod");
    }

}

public class ExtndClass extends AbstractClass{

    @Override
    public void abstractMethod() {
        // TODO Auto-generated method stub
        System.out.println(" am in extended class");
    }
    public static void main(String...arg){
        AbstractClass abs = new ExtndClass();
        abs.abstractMethod();
        abs.concreteMethod();

    }

}

Output:

am in extended class
am in concreteMethod