wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java doesn't support multiple inheritance rather it supports multi-level inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java doesn Watch "Java doesn New topic
Author

Java doesn't support multiple inheritance rather it supports multi-level inheritance

Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

We all know that Java does not support multiple inheritance rather it supports multi-level inheritance through the use of interface. Which means we can't extend from more than one class in Java. I have a question:
We know when we create a class implicitly it extends the class Object which is the super class for all Java classes. So when we are extending from another super class automatically it results in multiple inheritance (extending two classes...java.lang.Object and xxx.xxx.xxx. Isn't violation of Java language specification.


dkj4jdk
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all welcome to the JavaRanch. Can you please edit your post and remove the whitespace?

A class always extends only one class. That is the Object class or the class defined with the extends keyword. That class in turn can extend also only one class until eventual the Object class is reached.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
I considered responding to the original post, but I almost died of boredom while scrolling down through all the blank lines. I'm still recovering. Good thing Wouter is made of hardier stuff.

Please, don't put all those useless blank lines in your posts, in the future. Thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

I already removed them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
An odd definition of "already", but whatever. Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

The "already" was during or just after your post It was more a response to Wouter.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
Deepak Kumar Jena wrote:We all know that Java does not support multiple inheritance rather it supports multi-level inheritance through the use of interface . . .
Do we? I have never heard of multi-level inheritance. Where did you get that quote from?
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Interfaces constitute reference types. Interfaces in Java do support multiple inheritance.

Source: Java Language Specification.

"Multi level inheritance" sounds odd - especially when contrasting to "multiple inheritance" A single inheritance system can be "multi level" - and is, typically: JRE inheritance tree. Single inheritance, "multi level", class based - multiple inheritance with interfaces can't be expressed as tree, obviously.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
Deepak Kumar Jena wrote: . . . when we are extending from another super class automatically it results in multiple inheritance (extending two classes...java.lang.Object and xxx.xxx.xxx. . . .
No, that isn't multiple inheritance. You can only extend one class. Your class in that case does not extend java.lang.Object. It extends something else which . . . extends java.lang.Object. That isn't multiple inheritance at all.

And (sorry I didn't notice earlier) welcome to JavaRanch
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Even i am wondering about multi-level inheritance.
Otherwise java doesnot support multiple inheritance


SCJP6.0,My blog Ranchers from Delhi
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

When one class extends a super class, it inherits Object class via the super class... Class extending other Super Class does not directly inherit Object class... So its Multi-Level Inheritance...


SCJP 6 [SCJP - Old is Gold]
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Thanks to all for giving your valuable suggestions. But still now my doubt is not clear. My question is when we create a class does it extends from class java.lang.Object or not? Does it inheriting all the methods defined in class Java.lang.Object or not? If YES then when we are extending the same class from any other class then the class is inheriting methods from both the classes, which indirectly results in multiple inheritance. Kindly think about it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

if you're class does not explicitly have 'extends XXX', then your class DIRECTLY extends the Object class.

If you're class DOES have 'extends XXX', then it does not DIRECTLY extend the object class, but class XXX.

You then have to look at XXX, and see if it extends anything. If so, you look at it's parent class, etc.

Eventually, you will get to a class that does NOT have 'extends ABC' - and that class then inherits everything from Object. Since that class has those methods, it's children do, which pass them down to their children, until eventually we get to your class.

If you're great grandfather left a victrola to your grandfather, who left it to your dad, who left it to you, you did not inherit it directly from your great-grandfather, but you do eventually get it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Fred, if I am not wrong then all the classes whether it is library or user-defined extends the super class java.lang.Object directly. We can even call the methods defined in the Object class on any class. Again, how can a class extending XXX will extend java.lang.Object indirectly?......!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Maybe an example will help:
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Sir,
That means when the user-defined class is extending any other class than it is indirectly extending the class java.lang.Object. But I am sorry to inform you that in Sun's documentation no where it has mentioned (If you have the reference be kind enogh to forward it to me). Again I am wondering suppose at a certain point of time we want to know about the level of inheritance (in this case XXX as direct and java.lang.Object as indirect) then how can we trace it?
Once again one more question striking in my mind. In the case of multi-level inheritance suppose an interface is extending another interface. In this case how can we trace the level?
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Deepak Kumar Jena wrote: If you have the reference be kind enogh to forward it to me


It is late so maybe I misunderstood what you were requesting reference to, but here ya go:

Java 6 API wrote: Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.


http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Object.html


Everyday in every way, we get a little better.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4374
    
    8

This "multi-level inheritance" just means "inheritance". I don't see how you could define inheritance in a consistent way so that it doesn't work like that. It would have to mean that the IS-A relationship isn't transitive, which doesn't make sense.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
Apart from my doubt about whether "multi-level inheritance" is an entity:

If you look at the API documentation for any interface, for example SortedSet, it says what its superinterfaces are. A copy of its "extends" clause is also shown, so you can click on the links and follow the inheritance back.
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Need more clarification for inheritance involving an user-defined class and java.lang.Object. Please post answer with references.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Pick any class in the API (which can be found here). if you look at the top, it shows you it's exact hierarchy. For example, a StackOverflowError has this inheritance:



in other words, a StackOverflowError extends a VirtualMachineError, which Extends an Error, which extends Throwable, which extends....Object.

So, if you extend StackOverflowError, your final object is ultimately a child class of Object. If it makes more sense, it's a great-great-grandchild of Object, but nobody refers to it this way.

So, Throwable has everything that Object does, plus some more stuff.
Error has everything that Throwable does, plus some more stuff.
VirtualMachineError has everything Error does, plus some more stuff.
StackOverflowError has everything that VirtualMachineError has, plus more stuff.

and if you extend StackOverflowError, your new class will have everything it has, plus more stuff (whatever you add).
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Can we say like this.....!
The extends keyword is like a single inheritance ticket, when our class does not extends from any existing class it is passed to the java.lang.Object by our class as inheritance request. But as the class extends from any other class the ticket is passed to the super class rather than the class java.lang.Object. This process goes till the very super class which doesn't extends any other class and ultimately its inheritance ticket is with java.lang.Object. In this way the concept of singleton inheritance is mainteined with acquiring properties from both the classes.
santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
Can we say like this.....!
The extends keyword is like a single inheritance ticket, when our class does not extends from any existing class it is passed to the java.lang.Object by our class as inheritance request. But as the class extends from any other class the ticket is passed to the super class rather than the class java.lang.Object. This process goes till the very super class which doesn't extends any other class and ultimately its inheritance ticket is with java.lang.Object. In this way the concept of singleton inheritance is mainteined with acquiring properties from both the classes.


Deepak consider this:

class A extends class B.

class B will have Object class as super class yes. Next , Class A extends Class B, which means Class A is a subclass of Class B.
Class B has all public methods of Object class because, Class B is a subclass of Object.
Similarly, Class A has all public methods of Class B for same reason.
Now, Class A having all public methods (and anything else public) includes the methods Class B inherited from the Object Class. This means Class A has all public methods of Object class via inheritance.

and no, there is no multiple inheritance here, because Class A does not need to directly extend Object class as Class A already has all methods of Object class via inheriting Class B.


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Actually I think Deepak got it right with his last post, even if his wording was... unusual.
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Need some more suggestions from Campbell and Fred in this regard.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java doesn't support multiple inheritance rather it supports multi-level inheritance