Abstract class's can have a constructor, but you cannot access it through the object, since you cannot instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor.
Example
public abstract class AbstractExample {
public AbstractExample(){
System.out.println("In AbstractExample()");
}
}
public class Test extends AbstractExample{
public static void main(String args[]){
Test obj=new Test();
}
}
If interface & abstract class have same methods and those methods contain no implementation, which one would you prefer?
Obviously one should ideally go for an interface, as we can only extend one class. Implementing an interface for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass
No comments:
Post a Comment