Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstarct class can have constructor?  RSS feed

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can an abstract class can have a constructor? If so, What will be its importance? Since abstract is a class where we can't create its instance. In such scenario, what will be the use of constructor for the abstract class?
 
Tom Henner
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes- any class that inherits the abstract class will need that class to be constructed for it to inherit its members. And the abstract class may have implemented methods that its chidren may need.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, abstract classes can define constructors. The main reason to use abstract classes is to go some way to define the structure of classes that extend it. So if you decide that all child classes cannot use an empty constructor you could define a private empty constructor in your abstract class and all classes that extend it will now not be able to use a no args constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!