Which function is used to check if a class is a subclass of another class in Java?

In Java, as in other object-oriented programming languages, classes can be derived from other classes. The derived class (the class that is derived from another class) is called a subclass. The class from which its derived is called the superclass.

Which function is used to check if a class is a subclass of another class in Java?

In fact, in Java, all classes must be derived from some class. Which leads to the question "Where does it all begin?" The top-most class, the class from which all other classes are derived, is the Object class defined in java.lang. Object is the root of a hierarchy of classes.

Which function is used to check if a class is a subclass of another class in Java?

The subclass inherits state and behavior in the form of variables and methods from its superclass. The subclass can just use the items inherited from its superclass as is, or the subclass can modify or override it. So, as you drop down in the hierarchy, the classes become more and more specialized:


Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.
Now would be a good time to review the discussion in What Is Inheritance?
Which function is used to check if a class is a subclass of another class in Java?
.

Creating Subclasses

To create a subclass of another class use the extends clause in your class declaration. (The Class Declaration explains all of the components of a class declaration in detail.) As a subclass, your class inherits member variables and methods from its superclass. Your class can choose to hide variables or override methods inherited from its superclass.

Writing Final Classes and Methods

Sometimes, for security or design reasons, you want to prevent your class from being subclassed. Or, you may just wish to prevent certain methods within your class from being overriden. In Java, you can achieve either of these goals by marking the class or the method as final.

Writing Abstract Classes and Methods

On the other hand, some classes are written for the sole purpose of being subclassed (and are not intended to ever be instantiated). These classes are called abstract classes and often contain abstract methods.

The Object Class

All objects in the Java environment inherit either directly or indirectly from the Object class. This section talks about the interesting methods in Object--methods that you may wish to invoke or override.

Improve Article

Save Article

We know that inheritance is one of the building blocks of Object-Oriented Programming concept. It is the capability of one class to derive or inherit the properties from some other class. It also provides the reusability of code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.

issubclass() in Python

Python issubclass() is built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of given class else it returns False.

Syntax: issubclass( object, classinfo ) 

Parameters: 

  • Object: class to be checked
  • classinfo: class, types or a tuple of classes and types 

Return Type: True if object is subclass of a class, or any element of the tuple, otherwise False.

Example 1:

For issubclass python we are defining multiple classes and representing the phenomenon of Inheritance, then for a particular class we are checking whether it is the subclass for the mentioned base class or not.

Python3

class Vehicles:

    def __init__(vehicleType):

        print('Vehicles is a ', vehicleType)

class Car(Vehicles):

    def __init__(self):

        Vehicles.__init__('Car')

print(issubclass(Car, Vehicles))

print(issubclass(Car, list))

print(issubclass(Car, Car))

print(issubclass(Car, (list, Vehicles)))

Output: 

True
False
True
True

Python issubclass not working TypeError

Python3

print(issubclass( 1, int ))

Output:

Traceback (most recent call last):
 File "c:\Users\DELL\ranking script", line 36, in <module>
   print(issubclass( 1, int ))
TypeError: issubclass() arg 1 must be a class

Solution for python issubclass arg 1 must be a class TypeError, one should always pass a class instead of a non class type argument.

Python3

print( issubclass(type(1), int) )

print( issubclass(type('geeksforgeeks'), str) )

Output:

True
True

Note: Don’t get confused between isinstance() and issubclass() as both these method are quite similar. However, the name itself explain the differences. isinstance() checks whether or not the object is an instance or subclass of the classinfo. Whereas, issubclass() only check whether it is a subclass of classinfo or not (not check for object relation).

Related Articles,

  • Object Oriented Programming in Python
  • Inheritance in Python

Which function check if a class is a subclass of another class?

Python issubclass() is built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of given class else it returns False.

What is super () Java?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

What is subclass in Java?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

What happens when you make a new class a subclass of another class?

As a subclass, your class inherits member variables and methods from its superclass. Your class can choose to hide variables or override methods inherited from its superclass.