Solved

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

Posted on 2006-07-17
4
2,165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 70
glassfish admin console 1 47
restrict decimal places for double datatype 10 49
dao vs facade design patterns 2 69
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

732 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