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