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
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.
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.
This question already has answers here:
If one class is derived from another that is derived from Object, is that "multiple inheritence"
(7 answers)
Closed 7 years ago.
It may be very very basic question but didn't find any answer so far, so asking here. By default in java, every class extends Object class as far as i know. But again how we are able to extend any other class as multiple inheritance is not possible in java. Thanks in advance.
A class can't have more than one direct super-class, but it can have multiple ancestors.
For example, ArrayList extends AbstractList which extends AbstractCollection which extends Object. Even though ArrayList has 3 ancestors, it only has one direct super-class - AbstractList.
Object is parent class of all classes by default but as soon as you inherit your class from any other class now your class is not direct descendant of Object class any more and hierarchy goes up to the Object class.
Just to add to what Beri said, you can have a situation like this:
Class class extends Object implements Interface_1,Interface_2,Interface_3{
}
If you are trying to add more to the program, you can always add more classes and import their methods.
It should also be noted that the subclass does not inherit the private variables and methods from the superclass.
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.
This question already has answers here:
Do interfaces inherit from Object class in java
(9 answers)
Closed 9 years ago.
Means, by default in Java every class extends Object class.
So interface also extends Object class or not?
"by default in Java every class extends Object class. So interface also extends Object class or not?"
No...
1. interface can only extend another interface. in java every class extends object class (Not every interface).
If an interface has no parent, then it will IMPLICITLY have methods of the object class.
Reference types all inherit from java.lang.Object. Classes, enums, arrays, and interfaces are all reference types
(from http://docs.oracle.com/javase/tutorial/reflect/class/index.html).
So I think the answer is yes, it does.
Can you make instances of interfaces? : No
We need concrete classes which implement the interface which also implicitly extend Object class.
Also can you call methods defined in Object class in interfaces? : No(Not directly atleast. Maybe using it as a polymorphic reference.)
Interfaces and Objects are two different concepts. You cannot mix them. If anything that is common between them is that they are in general used a polymorphic references.
As for how does the code compile when we use interface as polymorphic reference
and call Object methods on it?
An interface has one implicit method declared for each public method in Object. For more details you can refer to this question.