Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a class is a subclass of other class?

Tags:

java

I'd like to check if a Class object represents a subclass of other class for example

Class class1 = Class.forName("Class1");
Class class2 = Class.forName("Class2");

 if(class1.isSubClassOf(class2)) // fake methos isSubClassOf
{
  // do sth
}

How can I implement this isSubClassOf method ?

like image 653
Łukasz Bownik Avatar asked Oct 01 '08 07:10

Łukasz Bownik


People also ask

How do you determine if a certain class is a subclass of another class Java?

The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.

Which function is used to check if a class is a subclass of another class Issubclass () Isinstance () Classmethod () list ()?

The isinstance() method checks whether an object is an instance of a class whereas issubclass() method asks whether one class is a subclass of another class (or other classes).

How do you know if a class is subclass of another class in Python?

Python issubclass() This function is used to check whether a class is a subclass of another class.

Can an object be a subclass of another class?

Can an object be a subclass of another object? A. Yes—as long as single inheritance is followed.


1 Answers

Class.isAssignableFrom() provides more-or-less what you're after, although it handle interfaces also, so may need to do a bit more extra work to be sure that it's a subclass, direct or otherwise.

like image 179
skaffman Avatar answered Sep 21 '22 18:09

skaffman