18

I need to check:

public static boolean check(Class<?> c, Class<?> d)
{
    if (/* c inherits from d */)
        return true;
    else
        return false;
}

How can I do that ?

And is that possible without c.newInstance() ?


The title was wrong at the first time. Now it's correct.
Bitterblue
  • 13,162
  • 17
  • 86
  • 124

6 Answers6

36

Use isAssignableFrom

if(d.isAssignableFrom(c)){
    // then d is a superclass of c
    // in other words, c inherits d
}

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Source

ThisClark
  • 14,352
  • 10
  • 69
  • 100
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
3

There is a method called Class#isInterface() in Class

 if (c.isInterface())
            return true;
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 2
    I didn't downvote but my first thought when I saw this answer was how does it check for inheritance or an implementation of a specific interface. Maybe the question changed on you, but if you reread it, the OP is looking to discover if one class extends another or implements a specific interface. Notice the `c inherits d` comment? – Kevin Bowersox Apr 25 '13 at 08:58
  • 1
    @KevinBowersox initially question title was different. :) – PermGenError Apr 25 '13 at 09:00
  • 1
    Thats what I suspected, your usually on point with good answers. – Kevin Bowersox Apr 25 '13 at 09:01
  • 1
    Down voting in not fair if question is changed after answer is posted ! – Apurv Apr 25 '13 at 09:15
  • 1
    @Apurv actually, its not the matter of the downvote.its just that the downvoter should leave a comment mentioning his reason . :) – PermGenError Apr 25 '13 at 09:19
  • @PermGenError I guess sometimes people show too much rush while evaluating an answer ! – Apurv Apr 25 '13 at 09:23
2

Try this out

if(c.isAssignableFrom(d)){
   return true;
} else {
   return false;
}
user2256686
  • 245
  • 1
  • 7
1

How about

public boolean isInterface();

According to docs:

Determines if the specified Class object represents an interface type. Returns: true if this object represents an interface; false otherwise.

Apurv
  • 3,723
  • 3
  • 30
  • 51
1
if (c.isInterface()) return true;

isInterface

public boolean isInterface()

Determines if the specified Class object represents an interface type. Returns: true if this object represents an interface; false otherwise. sAssignableFrom

public boolean isAssignableFrom(Class<?> cls)

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

Parameters: cls - the Class object to be checked Returns: the boolean value indicating whether objects of the type cls can be assigned to objects of this class Throws: NullPointerException - if the specified Class parameter is null. Since: JDK1.1

saeed
  • 2,477
  • 2
  • 23
  • 40
0

have you tried with

c.isInterface()???

from docs

Determines if the specified Class object represents an interface type.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307