This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why an Interface cannot have a Constructors ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why an Interface cannot have a Constructors ?" Watch "Why an Interface cannot have a Constructors ?" New topic
Author

Why an Interface cannot have a Constructors ?

devasis majhi
Greenhorn

Joined: Dec 29, 2012
Posts: 9
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
Bartender

Joined: Jan 17, 2008
Posts: 2519
    
  28

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

Joined: Oct 13, 2005
Posts: 41106
    
  29
And welcome to the Ranch
devasis majhi
Greenhorn

Joined: Dec 29, 2012
Posts: 9
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
Bartender

Joined: Jan 17, 2008
Posts: 2519
    
  28

You got it.

And 1 boogala point for the nitpick :-p
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why an Interface cannot have a Constructors ?