• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfave vs. Abstract  RSS feed

 
Tanzeem Akhtar
Greenhorn
Posts: 9
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi buddy, well i got that there is always a default constructor with abstract class. OK. But why not with interface?

Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tanzeem Akhtar:
Hi buddy, well i got that there is always a default constructor with abstract class. OK.

No there isn't. Abstract classes use the same approach to constructors as any non-abstract class - it only adds a default constructor if you haven't provided one. Nothing prevents me from writing only one constructor that takes a string, and there will be no default constructor without arguments then.

But why not with interface?

Because there is nothing to construct in interfaces. Instead, the constructor from the extended class is used. So in the case where you extend nothing explicitly, the Object() constructor is used, not the constructor from any implemented interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!