1

Consider this code

class Parent {

}

class Child extends Parent {

}

public class InheritenceExample {

    public static void main(String[] args){
        Parent p1 = new Child();
        System.out.println("the class name is "+ p1.getClass().getName());

    }

}

I declare p1 to be of type Parent, but assign it to a Child instance. when I do getClass().getName() I get Child as output. how can I get the declared type which is Parent in this case?

EDIT: This does not work in all cases: getClass().getSuperclass().getName()

consider this code

class Parent {

}

class Child extends Parent {

}

class GrandChild extends Child {


}

public class InheritenceExample {

    public static void main(String[] args){
        Parent p1 = new GrandChild();
        System.out.println("the class name is "+ p1.getClass().getSuperclass().getName());

    }

}

This ouputs Child but it must be Parent

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • Check here http://stackoverflow.com/questions/3294656/how-to-get-the-parent-base-class-object-super-getclass – Mahesh Guruswamy Nov 05 '13 at 21:20
  • 1
    possible duplicate of [How to find out the declared type of an identifier in Java?](http://stackoverflow.com/questions/11375096/how-to-find-out-the-declared-type-of-an-identifier-in-java) – user207421 Nov 05 '13 at 21:51

4 Answers4

0

Perhaps try to use getClass().getSuperclass()?

liltitus27
  • 1,670
  • 5
  • 29
  • 46
0

I thing there is no other method to find than just looping over superclasses.

Class<?> c = p1.getClass();

while (c.getSuperclass() != Object.class) {
  c = c.getSuperclass();
}

System.out.println("the class name is "+ c.getClass());
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
0
   getClass().getSuperclass().getName()

However never do this, this is very bad design

edit:

In your extended version you will need a loop

Parent superClass = p1.getClass();
while (superClass.getSuperclass() != Object.class) {
    superClass = superClass.getSuperclass();
}
superClass.getName();
Mr.Uyan
  • 113
  • 1
  • 9
Montaldo
  • 863
  • 8
  • 16
  • Well if you are implementing different interfaces, multiple inheritance etc. You do have to know what you are doing. getSuperclass calls the superclass above not the base class so to say. – Montaldo Nov 05 '13 at 21:27
  • you need ofcouse to initialise the superClass variable. – Montaldo Nov 05 '13 at 21:40
  • To answer I didn't test it, there might be a few error in there but that is in essence what you are trying to achieve. – Montaldo Nov 05 '13 at 21:46
  • This only works if the variable is declared as its superclass. – user207421 Nov 05 '13 at 21:50
-1

To get the immediate superclass

p1.getClass().getSuperClass();

However, in the event you are declaring an interface, you might use

        Class[] interfaces = p1.getInterfaces();
        if(interfaces != null){
            for(Class i : interfaces){
                System.out.println(i);
            }
        }
MadConan
  • 3,749
  • 1
  • 16
  • 27