We know that by default, every class inherits ultimately from the java.lang.Object class, which is why methods such as toString are readily available to every class. Therefore, a generic class is effectively as follows:
public class Foo extends java.lang.Object {
public Foo() {super(); }
}
However, is it at all possible to create a class which does NOT automatically inherit from the Object class, and thus, has no superclass? And if so, what would be the effect?
We can't write class without having java.lang.object as superclass. Compiler will automatically extend the object class.Only the object class itself and interfaces are the ones which do not extend object class.
No, that is not possible. 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.
I do not believe that you can have a class that does not inherit Object. Quoting from Object as a Superclass..
The Object class, in the java.lang package, sits at the top of the
class hierarchy tree. Every class is a descendant, direct or indirect,
of the Object class. Every class you use or write inherits the
instance methods of Object. You need not use any of these methods,
but, if you choose to do so, you may need to override them with code
that is specific to your class.
In cases where the inheritance is not explicitly stated, it is implicitly stated. Now, inheritance will obviously not form a cycle. The Object class is the only one which inherits from nobody, it's the top of hierarchy.
Related
I'm reading Java Inheritance Docs and got this paragraph, but I don't what is mean of this. Can any body explain?
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.
Edited
Another point related to this question:
And due to Object is superclass, NetBeans show me extra method list when I try to call any Member of class with object reference? All those methods are declared in Object class?
It means that,in java the Object class is the parent class of all the classes by default. In other words, it is the topmost class or the base class of java. All other classes inherit the properties of Object class.
Meaning of implicitly - suggested though not directly expressed.
If samething applied in Java classes. Consider there is a class called Test.
If you write
public class Test {....}
That is equivalent to
public class Test extends Object {....}
Though you didn't write it, it's equivalent to it. You need not to write extends Object manually. Internally JVM treats that your class is extends Object. Since the part after extends is your class, here Object is your super class.
But,
When you write
public class Test extends BigTest{....}
Things changed now. You are telling BigTest is my parent class. That means you are implicitly writing yourself that BigTest is my parent. Interesting part here is though BigTest is your direct Parent class, internally Object also your Parent.
So now you have 2 parent classes. One is BigTest which you mentioned and other is Object. If you didn't mention anything, only Object is your Parent.
Edit :
This is the reason that you will see extra list of method when you will try to call any member of class with object-reference. Those methods are declared in Object class.
why java do this? any feature of this?
Yes. There are benefits of it. Main reason is to reduce the code duplication.
Continue reading here .... Why Object as super class in Java ?
It means that the following two classes are the same:
class MyClass {
}
class MyClass extends Object {
}
If a class definition doesn't specify extends, the class implicitly extends Object.
The Object class comes with the JRE, you use any class without it. Java does this so you can have a reference to any object e.g.
Object o = x;
This works because no matter what x is, it is also an Object.
Note: even int[] class is a sub-class of Object.
When we declare a class without extending any class , does it have parent class in java?
yes and in almost all OO languages a class with no explicit extension, implicitly extends the base class, usualy called Object
reference: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
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.
So tha main problem is to understand the class hierarchy of Java.
We need to give an answer to this question:
"Explain whether in Java there is one single class hierarchy (with a single class at the top) or there are many class hierarchies (each with its own top-most class). Explain what consequences this has."
I have no idea about how to answer this question..
From the Object JavaDoc:
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.
Edit: Also take a look at the spec.
All objects in java inherit from Object
Its the Object class.
The class Object is a superclass (ยง8.1.4) of all other classes.
Also check this Oracle docs:
The Object class, in the java.lang package, sits at the top of the
class hierarchy tree. Every class is a descendant, direct or indirect,
of the Object class. Every class you use or write inherits the
instance methods of Object.
The Object class is mother of all java classes.
You can verify the same programmatically.
You will get null in the code below for Super Class of Object
Object.class.getSuperclass(); // returns null
The above test can be done for any other class similarly.
It is the Object Class. Every class is directly or indirectly descendant of Object class which is in java.lang package. Unlike any other parent/child class it doesn't require extends key word to inherit. It's implicit by nature where as you can write explicitly. Java compiler doesn't deny it.
You can use/call methods of object class from any other class and at the same time you can overwrite them to implement in your way.
Are there any classes that don't inherit Object as SuperClass or maybe have become Obsolete/deprecated?
According to Java Object superclass, java.lang.Object does not extend Object.
Other than that, all classes, i.e.
class ClassName {
//some stuff
}
implicitly extend Object class, if they don't extend any other super-class.
Interfaces, on the other hand, do not extend Object, as Interface, by definition, can not extend Class.
Also, Interfaces can not contain callable methods, nor can objects be instantiated from them. When interfaces are finally implemented, the implementing class will necessarily extend Object (and, no, Object doesn't implement or extend any other entity/class/interface).
According to java.lang.Object javadoc
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.
So, all objects in Java extends it directly or indirectly.
All Java classes inherit java.lang.Object (directly - by default, or via parents). If some class or method become deprecated with some platform release, it is always reflected in appropriate JavaDoc.
Are there any classes that don't inherit Object as SuperClass
There is exactly one of those, and it is java.lang.Object itself. Also all interfaces.
or maybe have become Obsolete/deprecated?
There are plenty of those. See the Javadoc.
We can use base class functions by extending from subclass.Generally we use equals() method which is defined in Object class.I read in the book that every class will extend Object class and so that we are able to use functions like equals() in our user defined class with subclass reference.
One doubt i am having is with out extending Object class (Even any other class that extends Object class) we are able to use equals method.
can any one explain how it happens?
Every class in Java extends Object by default(unexplicitly) no matter what you do.
If you do:
Class Foo { }
the compiler will treat it as if it were
Class Foo extends Object { }
Even when classes can only have at most 1 super class, if you do
Class Foo extends Bar { }
Class Bar extends Biz{ }
at certain moment up in the hierarchy one of those classes will have the class Object as it's parent, so even when only the topmost class would directly extend from Object, all of the other classes down the hierarchy would indirectly extend Object too, hence giving you access to Object class defined methods such as: clone, equals, finalize, etc...
Regards.
It's an implicit rule, JVM may implement the mechanism.