aspose file tools*
The moose likes Beginning Java and the fly likes constructor in abstract classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor in abstract classes?" Watch "constructor in abstract classes?" New topic
Author

constructor in abstract classes?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
hi,
I was wondering why there are constructor in abstract classes when you cannot make an object of it. Any particular use.
regards,
arun
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Abstract classes are meant to be subclassed, when an instance of a subclass is being created, the constructor of its superclass would be called firstly, so, you can think the superclass's constructor is a partial constructor of its subclasses.
Constructors are to initialise instances, if you find you are repeately doing same things of initialization in the subclasses of a superclass, you might move them to the constructor of the superclass. In another word, put the "common factor" of the constructors of the subclasses' to the constructor of the superclass. Therefore you not only save lines of code, your code is also semantically clearer, and of course, easier for you to maintain your code.
regards,
Ellen
[ July 24, 2003: Message edited by: Ellen Zhao ]
 
Consider Paul's rocket mass heater.
 
subject: constructor in abstract classes?