This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does JAVA support multiple inheritance or not

 
James Bellan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arulkumaran,

On a specific Interview i have been asked this question and till now i dont have any answers to get this one correctly / perfectly.

Q >> By default every class in Java inherits the "OBJECT" class , so any class is extended by this "OBJECT" class. If i try to extend some other class i think it is extended twice? Is this not multiple inheritance.

Eg: MyClass extends ParentClass (also extends Object class by default).
How this is managed / Or is this exceptional / How does JVM support only this inheritance?

--Naveen.
 
Don Stadler
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not multiple inheritance. It is a chain of single inheritance.

Put it this way: I can write class A which extends Object, Class B which extends A, and Class C which extends B. It's single inheritance all the way.
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly speaking Don is right, you can only specify one class after the extends keyword.

But when you add interfaces into the mix it is possible to engage in multiple "contracts" - similar in many ways to extending several abstract classes that have abstract methods with no implementation.

I've seen many a thread of discussion on Java multiple inheritance !
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer should be "yes and no"

Java supports multiple inheritance in interfaces but not in implementations.
If asked why no multiple inheritance of implementations, then the answer is simply "simplicity". Java follows the age of C++, but is a stickler for OOP principles. Creators of Java wanted to get rid of the complexity of multiple inheritance (of implementations) that is inherent in C++.

Hope it helps
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sue's answer is the one I (and most other hiring managers I know) look for when asking that question.

--Mark
 
James Bellan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all that answers Guys.
I am clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic