Inheritance in java and Superclasses(Object, Class

2020-01-27 06:41发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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.