• Post Reply Bookmark Topic Watch Topic
  • New Topic

why can we have constructor in abstract classes? while we can't instantiate an abstract class.???  RSS feed

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone!,
could anybody give me the reason that why can we have constructor in abstract class ...what is the advantage of this..
waiting for your valuable reply......
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class can contain data that needs initializing when a subclass is instantiated. That's what the constructor is for. A constructor is used to initialize an object, not create it.

When you create an object, and its constructor runs, that will call the constructor of its superclass, which calls the constructor of its superclass, all the way up to Object. That couldn't happen if there was a break in the chain when you have an abstract class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!