Reputation: 1673
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
?
Upvotes: 4
Views: 18321
Reputation: 3025
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.
Upvotes: 0
Reputation: 960
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
Upvotes: 3
Reputation: 101
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
Upvotes: 2
Reputation: 5794
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.
Upvotes: 0
Reputation: 19066
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
Upvotes: 0
Reputation: 22074
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.
Upvotes: 0
Reputation: 1197
The one and only that you are allowed to extend also extends the class Object
ultimately.Hence you are not extending twice.
Upvotes: 1
Reputation: 49352
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.
Upvotes: 8