Inheritance in java and Superclasses(Object, Class)

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:

  1. classes you write (custom classes), as well as
  2. 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.