• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Multiple inheritance

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In Java, every class is inherited from "Object" base class. So if my java class, (say, Myclass) explicitly extends a different class(say BaseClass).

class MyClass extends BaseClass {
...
}

Then isn't MyClass is inheriting both Object as well as BaseClass. If yes, then Isn't this a multiple inheritance. How does java internally handles it? Correct me if I am wrong.

Regards,
Sunil
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not multiple inheritance. It would be multiple inheritance if the class hierarchy wasn't a tree (exactly one parent). In your case, the direct parent of your MyClass is BaseClass, not Object.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's an unfortunate clash of terminology. When class A extends class B, it inherits all of B's fields and methods as well as those of B's parent, and B's parent's parent, and so on. However, as far as A is concerned, all of those fields and methods were inherited as a single unit directly from B. In that regard, it's single inheritence.

Multiple inheritence specifically refers to one class having two direct parents as opposed to a single parent and any number of grand-parents. In Java it would look like this:In the beginning it can seem somewhat arbitrary given that a class can implement multiple interfaces:However, implementing an interface doesn't involve the complexity of inheriting instance fields and method bodies. Instead, the interface merely provides a set of method signatures for which the implementing class must provide bodies (unless it's declared abstract).
 
Sunildatta Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank your for your comments. I think, some where I am missing some points to understand. Let me put my doubt in different way. Lets assume I have a class,

class MyClass {
....
}

By default, MyClass is a subclass of Object class (implicitly Every class is a subclass of Object class).

Now, if,

class MyNewClass extends BaseClass {
.....
}

Then, is MyNewClass extends both BaseClass and also Object class (implicitly).

please clear my doubt.

Regards,
Sunil
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Then, is MyNewClass extends both BaseClass and also Object class (implicitly).


Yes, but it is still not "multiple inheritence". That is when one class has two or more parent classes. MyNewClass has one parent (BaseClass) and one "grandparent" (Object).
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is multiple inheritance:



It's just a matter of definition of terms. The first situation presents all sorts of nasty issues -- for example, dealing with ambiguities if class C inherits a method named foo() from both A and from B. But in the second situation, there's no ambiguity at all: C will get B's version of foo().
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Class is a direct subclass of Object if you don't extend anything. If you extends a class the only direct superclass will be the extended one.

In your example MyNewClass extends BaseClass only, but since BaseClass extends Object, you can cast MyNewClass to an Object (what may have caused your confusion).

Hope it helps
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this example will help you to understand it more clearly.


See the result of the class compiler.

C:\work\java>javap A
Compiled from "A.java"
class A extends java.lang.Object{
A();
}


C:\work\java>javap B
Compiled from "A.java"
class B extends A{
B();
}

I think now its more clear.
 
Sunildatta Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. Sanju, your complied versions of java programs cleared my doubts.

Regards,
Sunil
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic