• 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

basic java question...please advice

 
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm learning Java. I have a parent class with some protected/default methods in it and I have a child class which is extending this Parent class. This child class also have overridden methods from that of its parent class. Both these Parent and Child class are in same package testClasses.
Now I have created another child class which is in different package called testClasses2.
Initial when I tried to create an object of both Parent and Child class located in testClasses package, the complier had shown me error. After that I imported those classes in this new child class as import testClasses.*; which enabled me to create their objects like Child child = new Child(); and Parent parent = new Parent();

My first question is do I need to import always like above in other classes in other packages (java fundamental concept) before I could create new objects, since Parent/Child classes were in same package and also child was extending the parent class is that why I did not get such error message?

Ok I have imported classes into my new child class(in different package) then why I'm not getting the proper string results(defined in methods) like "Vinod Kumar Nair" or "Vinod" by calling like child.getClass().getName() and parent.getClass().getName() ?
See output below

Parent.java:-


Child.java:-






Child2.java :-




Why I'm getting the following outputs:-
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:Ok I have imported classes into my new child class(in different package) then why I'm not getting the proper string results(defined in methods) like "Vinod Kumar Nair" or "Vinod" by calling like child.getClass().getName() and parent.getClass().getName() ?


Because for some reason you've put a getClass() call in there, which probably does something completely different than you expect. The getClass() method gets the java.lang.Class object of whatever you're calling it on. The Class object has a getName() method, which returns the name of the class. Note that the getName() method of the Class object does not have anything to do with your own getName() method defined in your classes.

You probably just wanted to call child.getName() and parent.getName(), not child.getClass().getName() and parent.getClass().getName().

Vinod Vijay wrote:Why I'm getting the following outputs:-


Exactly as above; because you're calling the getName() method of the java.lang.Class object, not the getName() method that you defined in your own classes Parent and Child.
 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

Vinod Vijay wrote:Ok I have imported classes into my new child class(in different package) then why I'm not getting the proper string results(defined in methods) like "Vinod Kumar Nair" or "Vinod" by calling like child.getClass().getName() and parent.getClass().getName() ?


Because for some reason you've put a getClass() call in there, which probably does something completely different than you expect. The getClass() method gets the java.lang.Class object of whatever you're calling it on. The Class object has a getName() method, which returns the name of the class. Note that the getName() method of the Class object does not have anything to do with your own getName() method defined in your classes.

You probably just wanted to call child.getName() and parent.getName(), not child.getClass().getName() and parent.getClass().getName().

Vinod Vijay wrote:Why I'm getting the following outputs:-


Exactly as above; because you're calling the getName() method of the java.lang.Class object, not the getName() method that you defined in your own classes Parent and Child.



Thanks...very well explained.....
Yes, it was my silly mistake and I will take care of this in future. I have learnt something from my mistake...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic