All classes inherit from java.lang.Object, although extends Object is (generally) not written out anywhere. How is this possible?
if you don't explicitly write extends Object the compiler does it for you. So knowing that a class can only extend one super class, the compiler will look at the hierarchy and extend the highest super class to Object. So every class will directly or indirectly inherit the Object class.
The Object class however is a special case because it doesn't extend anything.
Lastly if you were to compile a simple class and decompile it, you will see the compiler inserts extends
java.lang.Object (or
the bytecode equivalent)
into the class
The Object is implicitly direct/indirect super class of all class.
From Oracle Java doc:
Definitions: A class that is derived from another class is called a
subclass (also a derived class, extended class, or child class). The
class from which the subclass is derived is called a superclass (also
a base class or a parent class).
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.
Related
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.
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
I want to extend a class A and call the constructor of the class A from the child class B with the super() method. Also should the class A only be instantiated via the child class B. I can do that very simply by making class A abstract. But I read that I should only declare classes abstract, when they have at least one abstract method. Is there another way of making class A only be instantiated by the child class B by calling the super() method?
You should use abstract if there are certain implementations your child class needs, especially if you don't want the parent class to be instantiated ever!
However, in your case, I don't think there are any methods that need to be implemented or inherited, so you can just use protected.
Good luck!
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.
I wonder 3 things.
1: If I have implemented an interface (with a method) in a superclass where im declaring that method, and then I extend that superclass in another class. Then I don't have to redeclare that method right?
2: But if I don't declare that method in the superclass but in the child class then I instantiate the superclass. What happens then? It didn't contain any method from the instance.
3: Could you use implement in a class and then not declaring that method? Maybe it will be used as a superclass only for other classes to extend. And then just declare that method in the child classes or do you have to declare it in the current class you are implementing the interface?
Assuming you meant "reimplement" instead of "redeclare", that's correct.
Depends whether the class is declared abstract or not. If the superclass is abstract, then the child class either needs to implement it or to be declared abstract as well. If the superclass is not abstract, then it won't compile.
Declare the class abstract if you don't want to implement it.
More on this subject on Sun tutorial about interfaces and inheritance.
This is tagged for both PHP and Java - I'll answer for Java.
Right, it will inherit the implementation of the method from the superclass.
If the superclass still is declared as implementing the interface then it will cause a compile-time error, so this is not possible.
No, as I said before, if you implement an interface then you must implement or inherit the methods that that interface declares. The only exception is if the class is abstract, in which case you do not need to provide an implementation for the interface methods (which are implicitly abstract). Abstract classes cannot be instantiated.
Just adding to what has been already answered for the 2nd question (for Java):
As mentioned in other answer (by #BalusC), if your superclass tells that it implements an interface and doesn't provide implementation of method(s) in the interface then you have to mark the superclass as an abstract class. But you cannot instantiate the superclass since it is an abstract class.