Skip to content

Inheritance Relationship

When we obtain a Class object, we actually obtain the type of a class:

java
Class cls = String.class;

You can also use the getClass() method of the instance to obtain:

java
String s = "";
Class cls = s.getClass();

The last way to obtain Class is through Class.forName("") , passing in the complete class name of Class to obtain:

java
Class s = Class.forName("java.lang.String");

Class instances obtained in these three ways are all the same instance, because the JVM only creates one Class instance for each loaded Class to represent its type.

Get the Class of the parent class

With Class instance, we can also get Class of its parent class:

java
public class Main {
    public static void main(String[] args) throws Exception {
        Class i = Integer.class;
        Class n = i.getSuperclass();
        System.out.println(n);
        Class o = n.getSuperclass();
        System.out.println(o);
        System.out.println(o.getSuperclass());
    }
}

Running the above code, you can see that the parent class type of Integer is Number , the parent class of Number is Object , and the parent class of Object is null . Except for Object , any other non-interface Class must have a parent class type.

Get interface

Since a class may implement one or more interfaces, we can query the implemented interface type through Class . For example, query the interface implemented by Integer :

java
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws Exception {
        Class s = Integer.class;
        Class[] is = s.getInterfaces();
        for (Class i : is) {
            System.out.println(i);
        }
    }
}

Running the above code shows that the interfaces implemented by Integer are:

  • java.lang.Comparable
  • java.lang.constant.Constable
  • java.lang.constant.ConstantDesc

Special attention should be paid to: getInterfaces() only returns the interface types directly implemented by the current class, and does not include the interface types implemented by its parent class:

java
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws Exception {
        Class s = Integer.class.getSuperclass();
        Class[] is = s.getInterfaces();
        for (Class i : is) {
            System.out.println(i);
        }
    }
}

The parent class of Integer is Number , and the interface implemented by Number is java.io.Serializable .

In addition, calling getSuperclass() on Class of all interface returns null . To obtain the parent interface of an interface, use getInterfaces() :

java
// java.io.FilterInputStream,Because DataInputStream inherits from FilterInputStream
System.out.println(java.io.DataInputStream.class.getSuperclass());
// null,Calling getSuperclass() on an interface always returns null. To obtain the parent interface of an interface, use getInterfaces()
System.out.println(java.io.Closeable.class.getSuperclass());

If a class does not implement any interface , then getInterfaces() returns an empty array.

Inheritance relationship

When we determine whether an instance is of a certain type, normally we use instanceof operator:

java
Object n = Integer.valueOf(123);
boolean isDouble = n instanceof Double; // false
boolean isInteger = n instanceof Integer; // true
boolean isNumber = n instanceof Number; // true
boolean isSerializable = n instanceof java.io.Serializable; // true

If there are two Class instances, to determine whether an upward transformation is established, you can call isAssignableFrom() :

java
// Integer i = ?
Integer.class.isAssignableFrom(Integer.class); // true,Because Integer can be assigned to Integer
// Number n = ?
Number.class.isAssignableFrom(Integer.class); // true,Because Integer can be assigned to Number
// Object o = ?
Object.class.isAssignableFrom(Integer.class); // true,Because Integer can be assigned to Object
// Integer i = ?
Integer.class.isAssignableFrom(Number.class); // false,Because Number cannot be assigned to Integer

Summary

The inheritance relationship can be obtained through the Class object:

  • Class getSuperclass() : Get the parent class type;
  • Class[] getInterfaces() : Get all interfaces implemented by the current class.

Whether an upward transformation can be realized can be determined through the isAssignableFrom() method of Class object.

Inheritance Relationship has loaded