GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes basic java question...please advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic java question...please advice" Watch "basic java question...please advice" New topic
Author

basic java question...please advice

Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

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:-


Vinod Vijay Nair
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic java question...please advice