Solved

Java Reflection getting name of concrete class/subclass that implements an abstract class.

Posted on 2006-07-17
4
2,144 Views
Last Modified: 2012-05-05
Can I use reflection to get the name of the class that is implementing an abstract class in a situation like this:

MyAbstractClass c = new MyConcreteImpl();
Use reflection on c to get "MyConcreteImpl()".
Or better yet, use reflection to get a concrete version of MyConcreteImpl() that I could pass onto another function as an argument, so it would be the same as:

MyConcreteImpl c = new MyConcreteImpl();
myfunction(c)


In other words, to start with:
MyAbstractClass c = new MyConcreteImpl();

but before I get to "myfunction", use reflection to somehow find out the subclass that is being used and send this off to myfunction.


:-)  Did I confuse anybody?

0
Comment
Question by:ctangent
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17125986
yes:

Class clazz = c.getClass();
String name = clazz.getName();
0
 
LVL 92

Expert Comment

by:objects
ID: 17126076
getClass() retruns the class the the instanbce, not the class of the variable declaration.
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17127208
>>but before I get to "myfunction", use reflection to somehow find out the subclass that is being used and send this >>off to myfunction.

you can do that either using reflection or using instanceof operator


like,

if ( c instanceof  MyConcreteImpl )
 call the method,

or if ( c.getClass().getName().equals ( "MyConcreteImpl" )

 call the method,

i would prefer the instanceof operator


 
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17127436
use getClass() to figure out the class of variable c (as suggested by objects). So, in the cases where c is MyConcreteImpl, it would return class indicating MyConcreteImpl
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 69
Oracle SQL syntax check  without executing 6 56
tomcat not starting 6 45
Tomcat: Unable to run tomcat service. 2 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question