aspose file tools*
The moose likes Beginning Java and the fly likes Java Doesn't support Multiple inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Doesn Watch "Java Doesn New topic
Author

Java Doesn't support Multiple inheritance

sarada srinivas
Greenhorn

Joined: Oct 09, 2008
Posts: 20
Hi All,

As per I Know Java Doesn't support Multiple inheritance.

By default all the class extends Object class and still we are able to extends one more class to our class.



My Question is Class B had already extended Object class (default) and still able to extend Class A. Is this not Multiple inheritance. ?

Can some explain me clearly. I am little bit confused ?

I knew that Since Class B extend A. Class B cannot extend any other class still java Doesn't support Multiple inheritance.

Thanks,
Sarada
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Say you have two classes -- Class A and Class B. And they both extend from the Object class. Multiple inheritance is the ability to inherit from both class A and class B without making one inherit from the other.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Can some explain me clearly. I am little bit confused ?

There's nothing to be confused of Just think of the Object class as being a very special class, which is a superclass of all other classes. You don't have to extend it, it's done implicitly. From there, you can extends at most one other class. "Java does not support multiple inheritance" doesn't take into account the very special Object class.

[My Blog]
All roads lead to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Christophe Verré wrote:"Java does not support multiple inheritance" doesn't take into account the very special Object class.


Hmmm. I think that's a somewhat misleading explanation, as the OP might then ask, "But what about this:"



"Doesn't D extend C, B, A, and Object?"

I think the best way to explain it is to simply say that a class can directly extend only one other class. If you drew a diagram of Java inheritance, it always looks like a string of beads, but in C++ (for example) the diagram can look like any acyclic graph.


[Jess in Action][AskingGoodQuestions]
sarada srinivas
Greenhorn

Joined: Oct 09, 2008
Posts: 20

You all mean Java internally support multiple inheritance (Sincemy class has already extended Object class( implicitly) + class A which is multiple inheritance )

But from the developer perspective it resitcts multiple inheritance . Am I right ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ernest wrote:I think the best way to explain it is to simply say that a class can directly extend only one other class.

Thank you for the clarification.

Sarada wrote:But from the developer perspective it resitcts multiple inheritance . Am I right ?

Yes. Check Henry's example about what you cannot do, and Ernest's example about what you can do.
sarada srinivas
Greenhorn

Joined: Oct 09, 2008
Posts: 20
Thank you All

My confusion was cleared.

I belive what Christophe said is right

Java does not support multiple inheritance" doesn't take into account the very special Object class.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
sarada srinivas wrote:
You all mean Java internally support multiple inheritance (Sincemy class has already extended Object class( implicitly) + class A which is multiple inheritance )

But from the developer perspective it resitcts multiple inheritance . Am I right ?

No - it's not a question of "internally" vs "from the developer perspective". Both Java internals and developers need to understand that if A extends B and B extends C, then A extends C. The question is, what do people mean when they talk about multiple inheritance? From what I've seen, the term is always used to refer to multiple direct inheritance. A inherits directly from B, and B inherits directly from C. A inherits from C, but only indirectly. Each class has exactly one direct parent, except for Object, which has none. Classes may have any number of indirect parents. The thing that is different here between C++ and Java is that C++ allows multiple direct inheritance, and Java does not. So we talk about this difference as "multiple inheritance", and we don't usually bother saying that we mean multiple direct inheritance. Because multiple indirect inheritance was never controversial. We accept it and move on.

Looking back in this thread, I agree with EFH, and disagree with Christophe. The problem is that Christophe makes it sound like multiple inheritance is a special property of the Object class. What EFH is pointing out is that no, multiple indirect inheritance can be a property of any class, and multiple direct inheritance is not possible even with Object. It's a minor point, but I do think Christophe's statement is misleading in this respect.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Mike Simmons wrote:The problem is that Christophe makes it sound like multiple inheritance is a special property of the Object class.

I don't know where I made it sound like this. Can you enlighten me on this ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Christophe Verré wrote:
Mike Simmons wrote:The problem is that Christophe makes it sound like multiple inheritance is a special property of the Object class.

I don't know where I made it sound like this. Can you enlighten me on this ?


I don't think you did. From my reading, you simply said that if you don't declare a class as inheriting from anything, then the compiler will automatically have it inherit from the Object class.

The issue is how it was interpreted. Somehow, this became ... Multiple inheritance is not supported, but in the case of the Object class, java does support multiple inheritance. And that is just not correct.

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't know, this:
"Java does not support multiple inheritance" doesn't take into account the very special Object class.

does kind of make things confusing--because it still doesn't support multiple inheritance, whether or not you pay attention to the Object class.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Mike wrote:The problem is that Christophe makes it sound like multiple inheritance is a special property of the Object class. What EFH is pointing out is that no, multiple indirect inheritance can be a property of any class, and multiple direct inheritance is not possible even with Object. It's a minor point, but I do think Christophe's statement is misleading in this respect.

I had to read this a few times, and after reading Henry's comment, finally understood where the confusion came from. Bad wording can cause a hell of a confusion, sorry about that.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

David Newton wrote:I don't know, this:
"Java does not support multiple inheritance" doesn't take into account the very special Object class.

does kind of make things confusing--because it still doesn't support multiple inheritance, whether or not you pay attention to the Object class.

Yes, I got it. My bad.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No worries :)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
w00t! Glad it was sorted out.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11420
    
  16

sarada srinivas wrote:You all mean Java internally support multiple inheritance (Sincemy class has already extended Object class( implicitly) + class A which is multiple inheritance )

I would say this is wrong.

I would says your class does NOT extend the Object class. Your class ONLY extends class A. That's it - nothing else.

However, class A extends the Object class. So since your class is a type A, and A is a type Object, your object has everything a Object does.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

fred rosenberger wrote:I would says your class does NOT extend the Object class. Your class ONLY extends class A. That's it - nothing else.

+1
 
jQuery in Action, 2nd edition
 
subject: Java Doesn't support Multiple inheritance