2
  1. A class in Java cannot extend more than one class.

  2. Every class in Java extends java.lang.Object .

  3. From 1 and 2: Any class in Java cannot extend any other class other than java.lang.Object.

What is wrong in this deduction ?

2
  • 1
    you should look at number 2 as, Every class in Java is a subclass to java.lang.Object Commented Jan 30, 2010 at 18:15
  • And the comments beat me to it :) Commented Jan 30, 2010 at 18:20

4 Answers 4

7

You can only extend one class at a time. But A can extend B can extend C and so on.

Sign up to request clarification or add additional context in comments.

5 Comments

Beat me to it. You can also use Interfaces achieve many goals of multiple inheritance.
Lets say I have only one class. It already extends Object. Then I should not be allowed to extend any other class.
If it explicitly extends to Object by class A extends Object then it cannot extend to any other, but in Java everything is a subclass of Object's
@gameover: That's also not needed. You already implicitly inherit Object or virtually already get it from the extended class. In other words: extends Object is completely superfluous.
@gameover: not the case. What they mean by multiple inheritance is the ability to define a class as: public class C extends A, B {}
4

To extend on what Tangens said:

For number 2, it should instead read:

Every class that does not explicitly declare a class it extends extends Object

i.e. if you use the extends keyword, you are now saying that you explicitly extend something other than Object. However, at some point, that extension path will end up back at Object, if you follow the extension hierarchy.

The other part of this is that inheritance is really a chain - you have all of the properties of your parent and their parent and their parent's parent etc. Since the top of this hierarchy is always Object, you must, at one level or another, extend Object.

Comments

2

Correct statements:

  1. A class in Java cannot directly extend more than one class.

  2. Every class in Java extends java.lang.Object, directly or indirectly.

Comments

1

Inheritance is a tree. A class can only DIRECTLY extend one class, but that class could extend another which extends another, etc. So you can say A extends Object, B extends A, C extends B, etc. C inherits from Object indirectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.