java - How to find out the subclass from the base class instance? -


Is there a way to know the name of the class derived from the base class instance?

Example:

  class A {....} class B expanded a {...} class C is an expanded {...}  < / Pre> 

Now if a method returns an object of A , can I find out whether this type is B or C ?

either instanceof or class #getclass ()

  a returned = getA (); If {return instances b} {..} else if (for example C is returned) {..}  

getClass () any of these Will come back: a class , b class , C.class

if you need to clutter - I.e.

  ((b) returned) .doSomethingSpecificToB (); It is said, sometimes it is believed that using  instanceof  or  getClass ()  Bad practice should try to avoid the need to check the concrete subclass, but I can not tell you more with the information given to you. 


Comments