• 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

Printing Class Name

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I'm having a problem in printing the class names when inheriting and I'm pretty sure the solution is fairly simple.. To better explain myself, here is a sample code:



So to write the class names of Dog and Cat I am overriding the method type(). This is incorrect (well at least for what I'm trying to do it's incorrect). I want the classes Dog and Cat to inherit a method that allows them to print their type. How can I do this?

Thanks is advance
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Olivier Legat wrote:Hello everyone,

I'm having a problem in printing the class names when inheriting and I'm pretty sure the solution is fairly simple.. To better explain myself, here is a sample code:

-code-

So to write the class names of Dog and Cat I am overriding the method type(). This is incorrect (well at least for what I'm trying to do it's incorrect). I want the classes Dog and Cat to inherit a method that allows them to print their type. How can I do this?

Thanks is advance



You can use the java.lang.Object#getClass() method to get at an Object's class at runtime.
If you take a look at API of java.lang.Class, I'm sure you can come up with a way to get at the information you're after.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How about overriding toString() method for each of your classes . I can see you want a retrun String after all .
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jelle and Balu, problem solved
 
reply
    Bookmark Topic Watch Topic
  • New Topic