This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why an Interface cannot have a Constructors ?  RSS feed

 
devasis majhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why an Interface cannot have a Constructors ?

If the answer is we can not create an object of an Interface then why it not applicable to abstract class. Though we cannot create an abject of abstract class still we can define Constructors in side it which can be called from subclass constructors by using super keyword. Why cant we do the same for interfaces?
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The job of the constructor is to initialize the member variables. Abstract class can have member variables. Interface doesn't. So, Abstract class can have constructor. Interface doesn't need constructor. Nothing to intialize; No constructor required
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
devasis majhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The job of the constructor is to initialize the member variables. Abstract class can have member variables. Interface doesn't.


Not completely agree with it. Even Interface can have member but those are static and final by default which can not be initialized in a constructor. Probably that is is the reason we don't have constructors in Interface.
Correct me if I am wrong.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it.

And 1 boogala point for the nitpick :-p
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!