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

No members online now.

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top