How to tell which interface is implemented

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

Example,

If an object implements either :

javax.portlet.RenderRequest

or

javax.servlet.http.HttpServletRequest

how can I tell which it is?

I looked at obj.getClasses() no use provides a list of subclasses.
I looked at obj.getClassName() no use because it provides the
name of the concrete class not the interface.

Thanks
 
S

Sudsy

Example,

If an object implements either :

javax.portlet.RenderRequest

or

javax.servlet.http.HttpServletRequest

how can I tell which it is?

I looked at obj.getClasses() no use provides a list of subclasses.
I looked at obj.getClassName() no use because it provides the
name of the concrete class not the interface.
<snip>

Back to the books! The javadocs in this case. If you have an object
reference via the name obj then you can do the following:

Class k = obj.getClass();

Now research the Class#isAssignableFrom( Class k ) method.
Kewl, no?
 
M

Michiel Konstapel

Example,

If an object implements either :

javax.portlet.RenderRequest

or

javax.servlet.http.HttpServletRequest

how can I tell which it is?

Use instanceof:

if (object instanceof javax.portlet.RenderRequest) {
// process RenderRequest
} else if (object instanceof javax.servlet.http.HttpServletRequest) {
// process HttpServletRequest
} else {
// shouldn't happen
}

HTH,
Michiel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top