• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why an Interface cannot have a Constructors ?

 
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: 2756
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
Sheriff
Pie
Posts: 49367
62
  • 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: 2756
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic