• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already removed them.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An odd definition of "already", but whatever. Thanks.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "already" was during or just after your post It was more a response to Wouter.
 
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even i am wondering about multi-level inheritance.
Otherwise java doesnot support multiple inheritance
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe an example will help:
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need more clarification for inheritance involving an user-defined class and java.lang.Object. Please post answer with references.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I think Deepak got it right with his last post, even if his wording was... unusual.
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need some more suggestions from Campbell and Fred in this regard.
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic