954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiple Inheritence

I know java doesnot support multiple inheritence.lets say my class A extends B.Now my class A is extending class B as well as Object Class .how this happens?Why class A gets compiled?

sarath.koiloth
Light Poster
30 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

You can inherit from a single parent downwards as far as you want...
A extends Object; B extends A; C extends B; etc
Thus, by implication, C extends B, A, and Object.
What you can't do is this:
E extends F,G
ie. extend from two parents at the same level. This is what Java refers to a multiple inheritance.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

The language was setup so that you can't have multiple inheritance. If you want multiple inheritance, you have to use interfaces. By using keyword "implements" in the class heading.

extends - for classes
implements - for interfaces

jmaat7
Light Poster
29 posts since Sep 2009
Reputation Points: 10
Solved Threads: 4
 

Yeah interfaces are somewhat similar - if your class "implements" the interface, it forces you to include those methods that are defined in the interface itself.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Thanks for the replies.I want to know how these implementation happens.ie extending object class invisibly and extending to Other class explicitly.

sarath.koiloth
Light Poster
30 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

In effect, if your class does not explicity specify an "extends" superclass, then the superclass is taken to be Object. It's that simple.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

ok.So it is multiple inheritence right.extending to two classes.How jdk peoples implemented that ?

sarath.koiloth
Light Poster
30 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

It's single inheritance. Each class inherits from 1 class only, but that class, in turn,inherits from one class, etc, all the way up to Object.
When your code refers to a method or variable in a class Java looks for that method/variable in the class definition. If it finds it, OK. If not, it looks in the definition of the superclass. If it finds it there, OK. If not, it looks in that classes' superclass etc, all the way to Object. I it doesn't find it in Object you get a compile error.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Anyway its extending the object class. Or How these Object class is made super class to our class. How our class know that Object class is super class with out explicitly extending?

sarath.koiloth
Light Poster
30 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

That's part pf the language definition. If you don't specify an "extends" then your class extends Object. It's built into the compiler.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
That's part pf the language definition. If you don't specify an "extends" then your class extends Object. It's built into the compiler.


even if we specify extends to other class.It extends to object class.

sarath.koiloth
Light Poster
30 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

I'm not sure what distinction you are trying to make here. A class can only extend one class. That class, in turn, can only extend one class, etc all the way to Object.
Consider

class A {...}
which is exactly the same as
class A extends Object {...}
then
class B extends A {...}
class C extends B {...}
...
class Z extends Y {...}

It would be totally misleading to say "class Z extends Y and Object" without mentioning B..Y

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You