This question already has answers here:
Java doesn't support multiple inheritance but implicitly every class in java extends Object and allows one more [duplicate]
(8 answers)
Closed 10 years ago.
I have a query in java, that since Object class is the parent class for all, I want to know that is Object class is being implicitly extended by every class in java.
For example: if I make an object class B then I extend class A that is ok since in java, class can extend only a single class at most, but since my class B has implicitly extended the Object class then how can it extend class A? Please advise.
Only when a parent is not specified does it implicitly inherit off of Object, otherwise it only inherits off of the specified parent.
Side note:
This is allowed:
C
|
A
|
B
In the above case, replace C with Object to be in line with what you're asking.
This is not allowed:
C A
\ /
B
The inheritance can be as deep as you want it to be, but one object cannot have multiple parents.
Think of it as a family tree where individuals only have one parent. Each individual only has one parent, but all that parent's ancestors are also ancestors of the individual. That's what's meant when it is said that all classes extend Object: it is the most remote ancestor of all, though only the direct ancestor (parent) of those classes that are declared with extends Object or with no extends declaration at all.
I think you know the concept but you must be a bit confused. Think of a tree. Object class is the root of that tree. It has leaves and other nodes in between. No matter how your class hierarchy is designed, it will eventually an Object because its root is that. So you can extend any class (not abstract or Interface), without thinking of Object class. Because Object extention will be done automatically for you.
Here, I will suggest, the keyword is EXPLICIT. You can EXPLICITLY extend only one class. But if you don't do that Java will implicitly extend your class with Object class. If you have done that Object must be somewhere up in the hierarchy of the inherited class.
Related
This question already has answers here:
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 7 years ago.
(Just a theoretical question, I don't see a direct practical use)
Is there a way to do something like:
super.super.someMethod()
In Java?
super.someMethod()
exists, why not be able to invoke the super class of a super class?
(of course you can create a method:
protected void someSuperMethod(){
super.someMethod();
}
In the super class and invoke that in the child class having the same effect, but is there a direct way of doing that and, if not, why?)
S.
There is some ugly workaround, but they should not be used as it is bad practice.
When you inherit from an object it is fine, but you should not be able to access directly the parent of this object as changing it behaviour might also change the behaviour of your first parent.
The workaround you proposed is fine, as it is a method that is defined in the parent itself, therefore it does not change the behaviour as the parent access it own parent itself instead of the child accessing it grand-parent bypassing it first parent.
This is mainly why there is no direct way of doing this. If you want to access something in the grand-parent, then it should be done in the first parent instead, and in your child you tell the parent to access it parent.
Suppose you define an Object of type A, B and C.
B extends A, and C extends B.
Both A and B have the same method, with different validation.
Since C extends B, calling the method should use the validation of B, not A.
Using super.super you could call directly the method in A and skip the validation of B which would break the inheritance security/rule.
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.
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
Is java.lang.Object superclass of all the custom class/objects inherited implicitly? I thought java didn't support multiple inheritance. The reason I ask is if I already inherit from another class in my custom class and again java is forcing implicit inheritance of java.lang.Object on top of it, is it not multiple inheritance?
Also, is java.lang.class Class also the superclass for all custom classes/Objects? If not, how in java reflections we can get the type of class for any class passed or call isInstance on any object?
Every class without an explicit superclass inherits from java.lang.Object and every other class inherits from it indirectly because when you go up the inheritance tree, you will finally end at a class without an explicit superclass and then at Object.
java.lang.Class is the superclass of all class objects (not of all objects!), for example of String.class.
Is java.lang.Object superclass of all
the custom class/objects inherited
implicitly?
Minor correction: regarding the following expression:
superclass of all the custom
class/objects
A class A is a superclass of another class B. Objects or instances of B do not have A as their superclass (since they are objects/object instances, not classes themselves.) The most appropriate description is that these objects are instances of class B, which has A as its superclass.
Now, to your question on java.lang.Object. It is the superclass of everything that can be instantiated by the java runtime. That includes both:
classes you write (custom classes), as well as
classes provided by the Java language itself.
I thought java didn't support multiple
inheritance.
It doesn't. It only support single class (or implementation) inheritance (in conjunction with multiple interface (or type) inheritance.
You are confusing inheritance with class hierarchy. Pls see further below.
The reason I ask is if I already
inherit from another class in my
custom class and again java is forcing
implicit inheritance of
java.lang.Object on top of it,
And that's fine with multiple inheritance. Say you have a class B that you wrote inheriting from a class already provided by Java (say class A). So there is a linear, transitive relation as follows:
java.lang.Object > A > B
Where > stands for immediate parent of. Any class inheriting from Object can only have one and only one immediate parent. At no point you'll have a class C for which the following constrain for any two classes A and B does not hold:
forAll A, B { A > B -> forAll C { C >
B <-> C == A } }
Meaning, for all classes A and B such that A is an immediate parent of B, then for all classes C, C can only be an immediate parent of B *if and only if C is itself A.
With single inheritance you can have (theoretically) an infinite inheritance chain A > B > C > ... so long as every class in the chain is preceeded by one and only one immediate parent. As a useful analogy, this resemble the predecessor relationship in natural numbers (0 preceedes 1 preceedes 2 which preceedes 3 ...).
Disclaimer: This is not stuff that you use in day-to-day programming life activities. However, knowing precisely what these things mean under the hood will tremendously help you get through object orientation.
is it not multiple inheritance?
No (see above.) As opposed to single inheritance - explained above - in multiple inheritance you can have multiple immediate parents (as seen in C++).
As we noticed in the definitions above, the transitive inheritance relation '>' is from one class to another. But with multiple inheritance, the relation is from a set of superclasses {A} to a single class {B}:
{A} > B -> forAll a {a in A <-> a > B}
And in C++ where you can have classes without parents, then the set {A} can be the null set. In java, you also have multiple inheritance, but limited to interfaces (or types.)
Also, is java.lang.class Class also
the superclass for all custom
classes/Objects?
No. It will help your learning if you take a visit to the Javadoc manuals which are available online. Look it up. Really.
If not, how in java reflections we can
get the type of class for any class
passed or call isInstance on any
object?
Because instances of java.lang.Class (and all classes used by java reflections) are meta-data descriptors for other classes. This has nothing to do with inheritance. What you are referring to here is known as meta-programming.
Furthermore, java.lang.Class is a final class; ergo, it cannot be the superclass of anything.
The function of java.lang.Class is to be a meta-data provider/descriptor for all instances and subclasses of java.lang.Object (including java.lang.Class itself.) The class field associated to each class (.ie. String.class) is a static, class-level field describing the meta-data associated to that class.
The meta-data contained/described by java.lang.Class contains meta-data accessor objects for methods, constructors and fields. Again, this has nothing to do with inheritance.
Inheritance =/= meta-programming.
Hope it helps.
Everything is an Object, that said you could see the structure as this:
Object
Animal
Cat
and not as this:
Object Animal
Cat
Where Cat extends both, it's not like this last example, but it's Cat that extends Animal which extends Object.
Multiple inheritance means that one class has multiple direct predecessors. If A inherits from B, and B inherits from C, this is not multiple inheritance.
I don't understand the second question ("java.lang.class Class"); you might want to rephrase that to clarify.
Object is the superclass of every other class. When your Child class inherits from your Parent class, it is not the child of Object, but it is still a descendant, because its parent is a descendant of Object.
So if you don't specify a superclass/parent, it will automatically be Object. If you do specify a parent, then Object will still be a superclass, because it is by definition a superclass of the parent class.
Class is not a superclass of each class, but the Object class specifies a means to get each class's Class with the getClass method.
The custom class that you inherit from itself inherits directly or implicitly from Object, so you indirectly inherit from Object. The chain goes YourObject -> MiddleObject -> java.lang.Object. No multiple inheritance necessary.
Multiple inheritance is when you directly inherit from two unrelated classes. For example, if you have a class PoliceDog that inherits directly from both Canine and PoliceOfficer, that would be multiple inheritence.
In that case, it's not multiple inheritence but multi-level
inheritance.
For example, let's say you have a class say, Class Animal,
this class will be extending Object class, by default and extends no
other class.
Also let's assume, you have a class which extends the
Class Animal, say Class Lion. In this case, the Class Lion will not be
directly inheriting the Object Class but since it inherits Class
Animal which inherits Object class, it also indirectly inherits
it.