Inheritance of Object Class in Java - java

While i was reading java book, i came across "Every class extends class Object"...but if want a class B to extend class A.....but Class B will be now having multiple inheritance,one from Object class and one from Class A.How the conflict is resolved.
Can anyone explain?

Its multi-level inheritance, not multiple:
class A extends Object
class B extends A

First of all, Object class is the super/base/parent class of every class including user-defined classes.
So even if we don't mention it explicitly, the user-defined classes extends Object class by default.
Morevoer, Object class implements a set of methods and variables which are common to all the objects being created in the application. This is the main reason why we have Object class as a base class for all the other classes.
For eg:
hashCode() - this method creates a unique identity for each of the object being created in JVM.

ClassB extends from ClassA which also extends from Object. Therefore ClassB extends Object indirectly, through classA
"Every class extends class Object" just means if you don't specify the parent class, it takes on Object as the parent class

The book was trying to explain that every class is either a direct or indirect subclass of Object. Among other things, this means that every class inherits the public methods of Object: toString(), hashcode(), wait(), etc. It also means that whatever class variable a happens to be, you can always assign a to a variable of class Object.
There is no such thing as multiple inheritance in Java. The closest Java comes to that is interfaces, which is a whole subject in itself.

there is no conflict.. take a look at this structure
animal
bird
sparrow
parrot
dog
poodle
cat
the parrot class gets all attributes/methods of its super-class bird and from its super-class animal. this is called multiple inheritance.
You get traits from your parents right? You also get traits from their parents also.

Related

Default Superclass in Java

I was reading an article on Inheritance . Few facts were listed on Inheritance but one point i couldn't understand what is Default superclass and its explanation. What is Default superclass?
Default Superclass : Except Object class, which has no superClass, every class has one and only one direct superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of the Object class.
The default superclass is Object (more precisely java.lang.Object). If a class does not define a direct superclass explicitly (via extends), then Object is implicitly a superclass of that class.
Have a look at the following example which graphically shows this:
public class A {}
public class B extends A {}
public class C {}
Note that his rule does not apply to Object itself since this would produce a cyclic inheritance. In other words, java.lang.Object is the root of the class hierarchy.
Except Object class, which has no Super Class, every class has one and only one direct superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of the Object class
Points to understand first:
There is a class Object in java which is already available with the JRE libraries.
When you define a class without an extends keyword, your class will by default extend the Object class in Java.
Even if you extend another class in your new class, the parent or its parent transitively inherits the Object class.
Simple way to understand - after you define a class with / without a parent class, create an object for it. If you are using an IDE, you can see that there are some method suggestions which is not implemented in your class (or parent ). Methods like clone() equals() hashCode() wait() etc. Where did these methods/ behaviors come from to your object ? - Yes it came from the ultimate parent Object
The default inheritance is implicit and handled by the Java itself. Hope this makes your understanding better.
object class is base class for every class you create.
when you create an object of your class then constructor of object class is called.
The Object class in Java is the default superclass. Object class is inherited into a newly created class by default if it does not explicitly inherited from any other class. So every class that you create in Java programming is inherently a child class of the Object.
All classes in Java extend Object class by default, and they are subclass of object class, the only exception of this convention is the Object class itself, object class does not extend any class by default, this is the whole idea.

Multiple Inheritance in Java even if class inherits Object [duplicate]

This question already has answers here:
Java : If A extends B and B extends Object, is that multiple inheritance
(11 answers)
Closed 3 years ago.
All classes in Java are inherited from Object class by default.
Then how the class inherited can inherit other classes?
Java do not support Multiple inheritance, right?
Classes inherit from at least and at most one class,
Either from Object implicitly (without writing extends), or from other class explicitly (as extends YourParentClass)
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object
Because each class inherit from Object or from class inherit from Object, each class still inherit from Object, for example toString() method
If your custom class inherit from different class, still at the end of the hierarchy, the parent class will be Object, notice that classes hierarchy isn't multiple inheritance:
The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes
At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.
As you already correctly stated, all classes in Java are inherited from Object class by default. That means that any class that does NOT have an extends clause defined, implicitly has written extends Object. Example
class Animal // implicitly written extends Object
{}
On the other hand, if a class has an extends clause, then it does not directly inherit from Object. But its parent class inherits from object. As inheritance is transitive, therefore the class also inherits the functions of Object. Example
class Dog extends Animal {}
class Animal // implicitly written extends Object
{}
This means Dog is an Animal and Animal is an Object. Therefore Dog is an Object. You could write Object o = (Object) new Dog(); and that would totally be fine.
This can go arbitrarily deep. For example you could now have a class Poodle which inherits Dog which inherits Animal which inherits Object. Therefore a Poodle is an Object.
By using inheritance , a child class will inherit object class through parent class.
What you are talking about is known as - Multilevel inheritance.
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.
i.e. - A is Object class, B is the base class that you want to inherit and C is your child class.
btw Java 8 has introduced default method to handle basic multiple inheritance problem ( though I don't agree that it solves the actual problem) - check default methods in Java 8

Does Object class also extends some other class?

Every Class in java extends Object class. Does Object class also extends some other class?
Does Object class also extends some other class?
No. It is the first class in the hierarchy.
Quoting from the documentation:
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
no it is the first defined object in the hierarchy, you can see the hierarchy on the Object.java api.
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Regarding multiple inheritance in Java

If every class in Java implicitly extends Object class and multiple inheritance is not possible in Java then how do we even extend any class?
If you extend a class A that class in turn extends Object, so B extends A implicitly also extends Object.
"Java doesn't have multiple inheritance" means that you can't have two different parents, not that your parent can't have a parent.
C++ is an example of a language that lets you do multiple inheritance: http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/
Multiple inheritance looks like this:
class Teacher: public Person, public Employee
which means 'Teacher extends Person and Employee, inheriting its fields and methods'.
Instead of multiple inheritance, you're expected to create and implement interfaces to represent all the behaviours (or contracts if you prefer) your object supports. Java uses this for interfaces like Closeable and Serializable.
Multiple Inheritance is the concept of a single class inheriting from two or more super classes. If a class inherits from a super class and if that super class inherits from another super class it does NOT qualify as Multiple Inheritance. It is still Single Inheritance.
once u create object of sub class the object hierachy would be create on order of
objectclaass–>superclass—>subclass;
It is true that every class in Java inherits from the Object class – either indirectly or directly.
so in this case the sub class indirectly inherits object class.
Every Class that dosn't extends any other class it extends Object class.
if you extends anther class example extends Vector class
look to the hierarchy of class Vector you will end with a simple class that dosn't extends any anther class which explicitly extends Object.
and Any class extends anther class it explicitly extends all class that the parent class extends.
There is no multiple inheritance in Java, but there is class hierarchy. Inheritance in Java is transitive: if class A extends Object and class B extends A, than by transitivity A extends Object.

Multiple Inheritence Related Confusion in Java

I have confusion in java inheritance. As I studied Java does not support Multiple Inheritance. So that it does not have diamond problem of inheritance at all.
But, every class of Java inherits Object class by default, and if we extend a class then there are two inherited classes first is Object and second is our inherited class. As I know if any class inherits two or more classes, its known as Multiple Inheritance. It shows that java supports multiple inheritance because its inheriting two classes at same time.
Then why its said that java does not support multiple inheritance and is there any possibility to have diamond problem in java?
Since all classes extend Object, the base class you're extending is by definition already extending Object. Your class doesn't need to add Object itself since it already exists in the hierarchy.
That means, Object will still only exist once in your hierarchy as the very root, so no "diamond".
See this
OBJECT CLASS
|
|
CLASS A{} //Class A extends class Object
|
|
CLASS B{} //Class B extends class A
|
|
CLASS C{}//Class C extends class B
Since every Class is inherited from Object class it is not in the case When you inherit from other class.Thus the object class will become super or the root class for all other inherited classes.
So whats the problem?
But, every class of Java inherits Object class by default, and if we extend a class then there are two inherited classes first is Object and second is our inherited class
That's partially correct.
If you extend a class explicitely, then it won't extend Object class.
if you have a class
class A
{
}
then, compiler will change it into
class A extends Object
{
}
But, if you extends a class explicitely, as following,
class A extends AnotherClass
{
}
compiler won't add anything now, hence No multiple inheritance.
You have misunderstood the concept of Class extending Object in case of Inheritance.
Only the top level class in the inheritance hierarchy, extends from Object class, and rest of the class lower in the hierarchy, extend the immediate super class. Thus they have all the methods of Object class through this hierarchy of inheritance, and there is no multiple inheritance involved.
public class A { // Extends from `Object` class
}
class B extends A { // Extends from `A`
}
class C extends B { // Extends from `B`
}
So, there will be no diamond problem in the hierarchy.
Java supports only syntactical multiple inheritance. Java does not
supports implementation of multiple inheritance.
Some people says java supports multiple inheritance through interfaces but it's not correct here the explanation:
inheritance ::
Getting the properties from one class object to another class object:
Class A{}
Class B extends A {}
Here an object of class B getting the properties(methods/functions/ & data members/class variables)
of class A.
Why java does not supports multiple inheritance using classes:
Class A{}
Class B{}
Class C extends A,B{}
X--this statement causes error because Class A getting object of object class from both sides A and B...
Every java class by default extending Object of object class and Object of object class is the root object means super class for all classes.
But here Class C having two super classes of object so giving error means java does not support multiple inheritance by using classes.
Is java supports multiple inheritance using Interfaces:
Because of this interface concept only few of us saying that java supports multiple inheritance but it is wrong.
Here the explanation::
interface A{}
interface B{}
interface C implements A , B{}
(or)
interface A{}
interface B{}
Class C implements A , B{}
Here its look like multiple inheritance but inheritance means getting the properties from one class object to another class object.
Here in interface concept we are not at all getting any properties rather we are implementing the unimplemented methods of interface in class.
So inheritance and intefaces are quite opposite.......
So finally java supports only syntax of multiple inheritance does not supports implementation of multiple inheritance.
Inheritance is like debit and interface is like credit but interface has its own importance in other concepts like server side programming.
Not exactly in Java you can only inherit from 1 distinct class at a time. That distinct class might inherit from another class however you don't have a single class inheriting from multiple distinct classes...
You can have many ancestors but only one parent. Here ancestors mean Object class where as parent mean the class which you are extending.
By specifying a class to inherit you override the default extends Object. So only when you don't specify a class to inherit does a class extend Object.
The class you extend from already extends from Object. When you extend from the new class, you in effect stop extending directly from Object, and instead inherit it via the new direct ancestor.
So it's not that you're extending from two different classes, it's that you're extending from a class that itself already extends from another class, namely the Object class.
Visually, multiple inheritance would be:
A Object
| |
C
But the situation you describe is actually:
Object
|
A
|
C

Categories