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?

Edited 7 Years Ago by sarath.koiloth: n/a

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.

Edited 7 Years Ago by JamesCherrill: n/a

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

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.

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

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

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

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.

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?

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.

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.

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.

class A {...}
which is exactly the same as
class A extends Object {...}
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

This article has been dead for over six months. Start a new discussion instead.