Home » Java programming language

Java Class class asSubclass() method with example

Class class asSubclass() method: Here, we are going to learn about the asSubclass() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 04, 2019

Class class asSubclass() method

  • asSubclass() method is available in java.lang package.
  • asSubclass() method casts this Class object to denote a subclass of the class denoted by the given Class object.
  • asSubclass() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • asSubclass() method may throw ClassCastException at the time of casting a Class.
    ClassCastException: In this exception when this Class does not denote a subclass of the given class.


    public Class asSubclass(Class class_name);


  • Class class_name – represents the name of the class.

Return value:

The return type of this method is Class, it returns this Class object, cast to denote a subclass of the given Class object.


// Java program to demonstrate the example 
// of Class asSubclass (Class class_name) method of Class class

public class ParentClass {
    public static void main(String[] args) throws Exception {
        // Creating ParentClass and ChildClass instance
        ParentClass pc = new ParentClass();
        ParentClass cc = new ChildClass();

        // Get Classname
        Class parent = pc.getClass();
        System.out.println("Parent Class: " + parent);

        Class child = cc.getClass();
        System.out.println("Child Class: " + child);

        // We are denoting ChildClass of the given ParentClass object
        Class subclass = child.asSubclass(parent);
        System.out.println("child.asSubclass(parent): " + subclass);

class ChildClass extends ParentClass {



Parent Class: class ParentClass
Child Class: class ChildClass
child.asSubclass(parent): class ChildClass

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.