4

If Java can only extend one class, and every class extends java.lang.Object, how can it extend another class? And how come every class doesn't have written extends Object?

Maroun
  • 94,125
  • 30
  • 188
  • 241
splitgames
  • 1,669
  • 3
  • 13
  • 14
  • 1
    If you don't extend a class *explicitly*, you **directly** inherit the `Object` class. If you extend a class *explicitly*, you inherit everything from the *superclass*, which already extends `Object` (directly or not). – Maroun Jun 21 '13 at 18:15
  • See http://stackoverflow.com/questions/5003285/java-multiple-inheritance – Raedwald Jun 21 '13 at 21:57

8 Answers8

8

If java can only extend one class, and every class extends java.lang.Object, how can it extend another class?

When you say A extends B then it means that A extends B and B extends Object. One class can inherit from another which can inherit from another and at the top of the chain is java.lang.Object. Java doesn't support multiple inheritance , but supports multi-level inheritance.

how come every class doesn't have written "extends Object" ?

All classes extend Object implicitly , so it will be redundant coding.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
3

Every class in Java extends implicitly class Object, and you only have the permission to extend one more class, and infinite number of interfaces.

Same as having one default constructor for each class if you didn't write a constructor.

So when you say

class A{

}

it is actually looks like this:

class A extends Object{
    A(){
        super();
    }
}

But when you say when you say

class B extends A

In this case class B will have the features of class A, and because A extends Object, class B will also inherit class Object.

A -> Object
B -> A
So
B -> Object

Maroun
  • 94,125
  • 30
  • 188
  • 241
kdureidy
  • 960
  • 9
  • 26
2

Since java.lang.Object is the parent of every object in Java, you don't have to explicitly write extends Object

You can always extend any class. If you need to have behavior of 2 classes - you need to use interfaces for that

user2509983
  • 101
  • 4
1

The one and only that you are allowed to extend also extends the class Object ultimately.Hence you are not extending twice.

Priyanka.Patil
  • 1,177
  • 2
  • 15
  • 34
0

Multiple inheritance is not supported in Java, you can only inherit in a straight line (so to speak). You use interfaces though, which are IMO much more powerfull. I never needed multiple inheritance, but I make heavy use of interfaces. Basically instead of creating a second class, you define an Interface, and then let some class implement it. This can be a different class or the same.

All classes are already derived from Objects, you don't need to write that specifically.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

Each of the following can extend the object to the left:
Object -> Vehicle -> Car -> Coupe -> Camaro

So for example, Coupe is extending Car, and it is not (directly) extending Object... it's just that if you keep going up the chain you'll eventually get to Object.

You couldn't however, have Coupe extend both Car and Truck

Smern
  • 18,746
  • 21
  • 72
  • 90
0

Here is an example showing the class hierarchy of Java's java.lang.Number class.

http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html

As you can see, Integer class inherits from Number, and Number inherits from Object. So you can inherit multiple classes by chaining them.

cevaris
  • 5,671
  • 2
  • 49
  • 34
0

In object-oriented programming (OOP), inheritance describes a relationship between two types, or classes, of objects in which one is said to be a subtype or child of the other.

A -> B -> C -> D

The child inherits features of the parent, allowing for shared functionality. For example, one might create a variable class Animal with features such as eating etc.; then define a subtype Dog that inherits those features without having to explicitly program them, while adding new features like chasing cats.

Multiple inheritance allows programmers to use more than one hierarchy simultaneously, such as allowing Dog to inherit from Robot character and Animal and access features from within all of those classes.

        A
       / \
      C   D
       \ /
        E 

Java uses the first model and therefore inheriting from object and another class doesn't cause a problem because it is hierarchical in nature.

Object -> Animal -> Dog -> Pit Bull

The reason that every object does not need to extend Object is because it happens implicitly.

Zzz
  • 2,927
  • 5
  • 36
  • 58