Because an Abstract class is incomplete. See this example-
Now statement (1) will generate an error in Java. But if it had not, then what would happen at statement (2)? The call will try to call a method that has no body. Then what will happen? This is why you cannot instantiate an abstract class. However you can create a reference variable which has the abstract class as its type-
To instantiate this object you will have to create an instance of a concrete sub-class of the A class. That is you will have to create an object of a class that extends the A class and provides body to the display method like this-
Now the new code for the main method can be something like this-
The purpose of an abstract class is to group similar functionality together and leave the different ones to child classes which can then implement them as desired. Most books that teach Inheritancs and OO concepts would give the Vehicle example, with the Vehicle being the abstract base class that implement either partially or completely the characteristics common to all vehicles and leaving the rest to child classes such as for example, Car, Bike, Bus...... Hope this helps?
Cheers, Raj. [ August 12, 2008: Message edited by: Raj Kamal ]
Originally posted by AnkitJi Garg: Now statement (1) will generate an error in Java. But if it had not, then what would happen at statement (2)? The call will try to call a method that has no body. Then what will happen?
Abstract classes provide an intermediate facility between regular (concrete) classes and (totally abstract) interfaces.
If you are creating a related family of classes, you can avoid maintaining duplicate code by pushing implmentations as far up the class tree as you can to their common origins. You can leave methods abstract for which you want polymorphism but for which you do not yet have enough specificity for a meaningful implementation. An abstract class is a "partially implemented" interface.
Take note that any class with an abstract method MUST be abstract, but you may declare a class abstract even if none of its methods is abstract.
In no case, may you instantiate an abstract class.
Joined: Dec 31, 2007
If there is a abstract class then why we need interface.The abstract class itself satisfied our needs know.
But you cannot inherit two abstract classes but you can inherit two interfaces. For example there's a class Manager. The employee is an Employee as well as a Father or a Husband...Then the Manager class cannot look like this