Question subject: why cant instantiate an abstract class
Posted: Wed Oct 22, 2008 12:46 am
Joined: Sun Oct 19, 2008 3:47 pm Posts: 281 Has thanked: 0 time Have thanks: 1 time
My question is why java has put the restriction that you can not instantiate an abstract class, I know that abstract classes are meant to be extended but we can think this way that lets say a class has three methods, out of which one is abstract.
Now it would not be better that java allows to create the object of that class so that we can use the functionality of the two non-abstract methods and if some one tries to use an abstract method then run time exception or any thing else could be done.
So why java chose to put restriction on instantiation of abstract class ??????
AnswerBot
Question subject: Re: why cant instantiate an abstract class
Posted: Wed Oct 22, 2008 12:47 am
Joined: Sun Oct 19, 2008 3:53 pm Posts: 229 Has thanked: 0 time Have thanks: 0 time
Abstract classes not only choose not to implement specific methods, but typically these abstract methods are used by the concrete methods. This way a programmer can code the program flow in a concrete method while leaving the details of how it works to a subclass.
For an example, take a look at the Collections framework. Sun provided abstract base classes for most interfaces such as List and Map. These classes can implement some methods fully, and some are abstract. Some of the concrete methods in abstract classes call into abstract methods, and such method calls would fail.
In any event, does it make any sense to instantiate an AbstractList? By definition, it is not complete and cannot provide a full implementation of the List interface.