Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor in abstract class

 
mohan shetty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Actually what is the use of having a constructor in an abstract class?
 
Shiva Shankar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when sub class implementing the abstract class,before subclass constructor is invoked, it calls the constructor in abstract super class.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned, a constructor is required in an abstract class, to allow the concrete subclass to be instantiated.

However, not all possible access modifiers make sense for the constructor of an abstract class. The "public" modifier doesn't in practice offer any more access than "protected". The "private" modifier makes instantiation of a concrete subclass impossible; this only makes sense for a non-instantiable utility class (which is not very OO).

Normally, therefore, the constructor of an abstract class will be "protected". Occasionally, it might be package-private.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic