• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is Java Single inheritance or multiple inheritance

 
lakmal vithanage
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In many places, it is mentioned that java is a single inheritance programming language. But we can implements many interfaces to a class and then inheritances from more interfaces.so my problem is whether Java is Single inheritance or multiple inheritance.and please explain why.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java supports multiple inheritance of interface (or type), which is the main reason to use inheritance in the first place.

It supports only single inheritance of implementation.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can IMPLEMENT many interfaces. But you can only EXTEND one class.

I am not the expert here by any means, but to me, inheritance means "extends". You get stuff for free when you say "public class MyClass extends SomeOtherClass". You get methods.

When you say "public class MyClasss implements SomeInterface, SomeOtherInterface", you don't. You have to then go and implement the methods.

That's how I see the difference.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:You can IMPLEMENT many interfaces. But you can only EXTEND one class.

I am not the expert here by any means, but to me, inheritance means "extends". You get stuff for free when you say "public class MyClass extends SomeOtherClass". You get methods.

When you say "public class MyClasss implements SomeInterface, SomeOtherInterface", you don't. You have to then go and implement the methods.

That's how I see the difference.


I don't see it that way.

There's really no fundamental difference between "implements" and "extends". They both specify inheritance, and IS-A relationship. It just so happens that the language designers decided to make a distinction in the keywords used. There's no real difference between extending a purely abstract class and implementing an interface.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: There's no real difference between extending a purely abstract class and implementing an interface.

But there is a difference between extending a concrete class and extending an interface. whipping these up (so they may not be 100% correct):





This should compile and run:

This will not:


Because I have to DO something to myClassB. I have to write the code to make this a viable class.

Maybe it is a distinction without a difference. And again, I'm not an expert here. But this is how I see the difference.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Jeff Verdegan wrote: There's no real difference between extending a purely abstract class and implementing an interface.

But there is a difference between extending a concrete class and extending an interface. whipping these up (so they may not be 100% correct):
...
Maybe it is a distinction without a difference. And again, I'm not an expert here. But this is how I see the difference.


But that extends/implements difference is exactly the same as the difference between extending a concrete class and extending a class with only abstract methods. It's not really a difference between extends and implements, but rather between whether you're inheriting from a concrete type or an abstract type.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic