Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Is it possible to check if one class is extending another in java without making instances?

I have two Class<? extends SomeClass> variables, one obtained from looping the keys of a HashMap, we'll call it A, and one passed as a parameter, which we'll call B. I'd like to see if B extends A.

So, as I currently understand things, I'd make an instance of B (using createInstance) and check instanceof. But this is not only pretty slow, but cumbersome as classes need an empty constructor. See here and here, where they discuss isAssignableFrom and getSuperClass, but isAssignableFrom (from what I read there) doesn't work on the same class (SomeClass.isAssignableFrom(SomeClass) ?), and getSuperClass doesn't walk up the tree.

Is there a way I can check if B extends A without creating an instance of B?

For example:

class SomeClass {}
class ExtendingClassA extends SomeClass {}
class ExtendingClassAB extends ExtendingClassA {}
class ExtendingClassB extends SomeClass {}
boolean ClassExtendsClass(Class<? extends SomeClass> A,Class<? extends SomeClass> B) {
    return A.class instanceof B.class;
}
ClassExtendsClass(SomeClass.class, ExtendingClassA.class); //true
ClassExtendsClass(SomeClass.class, ExtendingClassAB.class); //true
ClassExtendsClass(SomeClass.class, ExtendingClassB.class); //true
ClassExtendsClass(ExtendingClassA.class, ExtendingClassB.class); //false
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Yup. Generally, check the API (javadoc) of the type you think would be the right place for it. In this case, that'd be Class itself, and, luckily, it's there:

Class<?> a = Integer.class;
Class<?> b = Number.class;
System.out.println(b.isAssignableFrom(a));
System.out.println(b.isAssignableFrom(b));
> true
> true

but isAssignableFrom (from what I read there) doesn't work on the same class

You should test that stuff before making assumptions. It works fine.

over 4 years ago · Santiago Trujillo Report

0

you could use the getSuperclass method for a class object recursively until you have no more superclasses:

class A {}
class B extends A {}
class C extends B {}

public class Main {
    public static void main(String[] args) {
        Class clazz = C.class;

        System.out.println(clazz.getClass());
        System.out.println(clazz.getSuperclass());
        System.out.println(clazz.getSuperclass().getSuperclass());
        System.out.println(clazz.getSuperclass().getSuperclass().getSuperclass());
        System.out.println(clazz.getSuperclass().getSuperclass().getSuperclass().getSuperclass());
    }
}

the above example will produce

class java.lang.Class
class B
class A
class java.lang.Object
null
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!