I have a class that extends to another class and that class extends to another class.
class 1 extends class 2 class 2 extends class 3 class 3 extends class 4 class 4 extends class 5 class 5 extends class 6
Now I want to find all super classes of class 1.
Anyone know how I could do that in java?
The class named Object is the super class of every class in Java.
The class from which the subclass is derived is called a superclass (also a base class or a parent class). Excepting Object , which has no superclass, every class has one and only one direct superclass (single inheritance).
To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass.
A: The Object class, which is stored in the java. lang package, is the ultimate superclass of all Java classes (except for Object ).
Use Class.getSuperClass()
to traverse the hierarchy.
Class C = getClass(); while (C != null) { System.out.println(C.getName()); C = C.getSuperclass(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With