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.
Related
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.
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.
class child
{
child()
{
super();
System.out.println("Hello");
}
public static void main(String arg[])
{
child obj=new child();
}
}
In this code when I a create an object of class child , the child constructor is called. But why is it not giving error as there is no parent class. What does the super() do here?
Whose constructor is the super() keyword calling?
In Java every object implicitly extends Object. Calling super here will just call Object's constructor. On another note you should really abide by naming conventions such as capitalizing class names.
It is calling the constructor of the Object class as all object in java extends by default to the Object class.
From 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.
You're invoking the Object method, from which all other classes descend eventually.
First up, clarifying the class hierarchy in this situation, the inheritance section in the Java tutorial states:
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.
Then, for the tutorial stuff on using super:
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
Every class we create in Java is an implicit descendant of "Object" object (in other words, subclass of Object). Hence when ever you are making a call to super() it implicitly calls the constructor of Object class. The basic reason to have this feature is to generalize the common features like:
synchnronizaton - like wati()
object identity - like hashcode(), equals()
and many more.
Thanks,
JK
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.
All objects, including arrays, implement the methods of this class. Thats why super() in your case is actually calling the constructor of Object class.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Classes that don’t inherit Object class
I am wondering if each class in Java is inherited from Object implicitly. I am reading a book which said no, I got quite confused.
That book is lying. Take a look at any class from the Java API and you'll notice the very first parent class is, in fact, Object.
BTW : what is that book anyway?
** Edit **
Let's clarify one thing first :
Q: Does every java class inherit from Object class implicitly?
A: If you consider that the class Object cannot have a superclass "because it is the primordial class and has no direct superclass", then no, therefore your book was right.
However, if you consider that "the class Object is a superclass of all other classes", and is excluded from the question (as a class cannot inherit itself), then by definition yes.
From my understanding of the language and it's specifications, is that "all class and array types inherit the methods of class Object" should be taken litteral. Meaning that every object created possess the methods defined in the class Object. For this reason, and since the class Object has no superclass (because there is no superclass from the specification!), and also since all class inherits those methods (the methods are available in any object created by the JVM), then if a class does not explicitly extends Object (because it's not the class Object), it does so implicitly by itself or from a superclass by the virtual machine.
Quoting the documentation:
public class Object
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.
As you can see... Class inherit from Object:
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html
public final class Class<T>
extends Object
implements Serializable, GenericDeclaration, Type, AnnotatedElement
throw this book to Trash!!
The book can be correct in one case. The Object class doesn't extends Object. But remember every other does.
See documentation.
Class Object
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.