Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor in abstract classes?

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic