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?
The custom class that you inherit from itself inherits directly or implicitly from
Object
, so you indirectly inherit from Object. The chain goesYourObject -> 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 bothCanine
andPoliceOfficer
, that would be multiple inheritence.Object
is the superclass of every other class. When yourChild
class inherits from yourParent
class, it is not the child ofObject
, but it is still a descendant, because its parent is a descendant ofObject
. So if you don't specify a superclass/parent, it will automatically be Object. If you do specify a parent, thenObject
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 theObject
class specifies a means to get each class'sClass
with thegetClass
method.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.
Minor correction: regarding the following expression:
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:It doesn't. It only support single
class
(orimplementation
) inheritance (in conjunction with multipleinterface
(ortype
) inheritance.You are confusing inheritance with class hierarchy. Pls see further below.
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:
Where
>
stands forimmediate parent of
. Any class inheriting from Object can only have one and only oneimmediate 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: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 animmediate 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 byone and only one
immediate parent. As a useful analogy, this resemble thepredecessor
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.
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}
: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 tointerfaces
(or types.)No. It will help your learning if you take a visit to the Javadoc manuals which are available online. Look it up. Really.
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.
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 atObject
.java.lang.Class
is the superclass of all class objects (not of all objects!), for example ofString.class
.Everything is an Object, that said you could see the structure as this:
and not as this:
Where
Cat
extends both, it's not like this last example, but it'sCat
thatextends
Animal
whichextends
Object
.