Saturday, 4 February 2012

How to Instantiate the Abstract Class

An abstract class cannot be instantiated directly. An abstract class has to be sub-classed first and then instantiated. Only then the method defined in the abstract class can be invoked.


Sample Code snippet


public abstract class AbstractEx {
public void one()
{
System.out.println("1");
}
public void two()
{
System.out.println("2");
}
}


public class AbstractExample {
public static void main(String[] args) {
AbstractEx a= new AbstractEx() {
};a.one();
}
}

No comments:

Post a Comment