• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to know if one is extends other's class

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have two objects and i want to know if one object is extends or implements the other object class or interface.
how do i do it?

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to look at isAssignableFrom method.
Here is an example:


Edit: Note that you can use the same method to check if a class implements an interface, as noted in the API.
 
david dabush
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o.k
thank you its working
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even for objects of classes that you declare, you can use the same method to check if one extends another, or if one implements interface. Just instead class name use subInstance.getClass(), everything else is the same.
You can also use getSuperclass() method to see if your class or instance of it extends from another class, and getInterfaces() to list all the interfaces your class implements.

Edit: You changed your response in the meantime, so now I see the first answer is suitable for you.
 
david dabush
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have to classes names as strings.
and i want to know if one class extends or implements the othe class;

exapme:
String class1Name, class2Name;

how to know if class class1Name extends or implements class2Name?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

david dabush wrote:thank you its working


Maybe it is, but have you asked yourself: Why do I need to know this?

It smacks to me of reflection which, as far as I'm concerned, is either
(a) a last resort
or
(b) suggests bad design

Java is staticallly-typed and reflection is an aberration. Use it at your peril.

You have been warned.

Winston
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

david dabush wrote:i have to classes names as strings.
and i want to know if one class extends or implements the othe class;

exapme:
String class1Name, class2Name;

how to know if class class1Name extends or implements class2Name?


In that case check forName method of Class class.
 
david dabush
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and how i use reflection so i can turn strings into class?

can you give example?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don’t need reflection.
And here are two ways of getting a Class object from its name rather than from an instance:-Count the compiler errors before running that code.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you even try to read the API that I pointed you to at the beginning? This is one way to do it:
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic