File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing Class Name

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jelle and Balu, problem solved
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic