How come a super class method have a subclass object?
sarang bharambe
Ranch Hand
Joined: Apr 01, 2005
Posts: 40
posted
0
While studying for SCJP I came across this Code and I can't find the logical answer for it.
I thought that the code should not get compiled but when I tried to compile SuperClass.java then SubClass.java also get compiled automatically. How come this is possible? also Why SuperClass.java get compiled?(I thought It should give cannot resolve symbol error at //line 1)
Can anyone explain this with the Flow of compilation?Its too confusing
Thanks in advance sarang
SCJP 1.4 (86%)<br />SCWCD 1.4 (86%)<br />----------<br />If You dont succeed at first time,call it Version 1.0 !!!
Akash Bhatt
Greenhorn
Joined: Jul 26, 2005
Posts: 25
posted
0
hi sarang thr's no confusion at all.. the method 'aMethod()' though of superclass but is called by a object of subclass ie the method resides in the scope of that subclass object.... so u must not think that its the superclass who's doing it... also as u see any subclass object can be instantiated in superclass just like other classes can be (After all its a class only...).
Also the object instantiated in superclass is local to that method which is lost once u get out of that method ....
output may indicates so
Output: In Super aMethod In Sub Print In Super Print1
the last line indicates that the first object 's1' is still alive...
If it is not inheritance relationship then the compilation of the other class is understandable but in this case the compilation is interleaved
so any more explanations???
haritha yellapu
Greenhorn
Joined: Sep 06, 2005
Posts: 9
posted
0
hi Sarang! Actually i am not able run this code: i am getting the following error. i am just a beginner can u plz. help me.
superclass.java:2: class SuperClass is public, should be declared in a file name d SuperClass.java public class SuperClass{ ^ superclass.java:13: class SubClass is public, should be declared in a file named SubClass.java public class SubClass extends SuperClass{ ^ 2 errors
regards, Haritha.
sarang bharambe
Ranch Hand
Joined: Apr 01, 2005
Posts: 40
posted
0
welcome to the javaranch..
Can u put both SuperClass and SubClass in different files and then try to compile?
hope this will help
haritha yellapu
Greenhorn
Joined: Sep 06, 2005
Posts: 9
posted
0
Thanx Sarang! but still i am helpless
it throws the following " superclass.java:3: class SuperClass is public, should be declared in a file name d SuperClass.java public class SuperClass{ ^ 1 error "
Manoj Kumar Sikhakolli
Greenhorn
Joined: Sep 22, 2005
Posts: 15
posted
0
Halo haritha,
What hav u given as the file name in which this superclass exists?
There can be any number of classes in a java file but only one should be public and the name of the file should match the public class writen.
Hope this would help u
You have compiled the file named superclass.java and the public class name is SuperClass. Match the cases(uppercase for S and C) [ September 28, 2005: Message edited by: Manoj Kumar Sikhakolli ]
haritha yellapu
Greenhorn
Joined: Sep 06, 2005
Posts: 9
posted
0
hey oooops sori yaar. i was not using Caps S n C there in cmd. now its fine .
thanx a lot!
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
posted
0
Hi haritha,
What manoj is trying to say is that..
you should define and save the superclass in a separate file with the same name as class name...since this is a class with specifier public.
Similiarly ...for subclass...
And then try..
haritha yellapu
Greenhorn
Joined: Sep 06, 2005
Posts: 9
posted
0
yeah! i could execute the code n got the same o/p as Akash got is the flow like this?
from the main method of Subclass.java public static void main(String[] args) { SubClass s1=new SubClass(); s1.aMethod(); //so here an object of the class: Subclass is created (s1) and the Superclass method :aMethod is called so it executes n prints "In Super aMethod" and there System.out.println("In Super aMethod"); SubClass s1=new SubClass(); s1.print(); //so it executes print() method n prints "In Sub Print"then back to the main method to this statement s1.print1(); //executes n prints "In Super Print1" i suppose this is the flow.plz let me know?
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
posted
0
Hi,
Yes you are right about the explanation...
Regards
sarang bharambe
Ranch Hand
Joined: Apr 01, 2005
Posts: 40
posted
0
Thats fine guys...
But you all are explaining the flow at runtime and my post asks for the flow during compilation.
I guess the possible flow is:
1.The compilation of SuperClass starts 2.when It Comes to the line SubClass s1=new SubClass(); then the compiler try to find the Class file SubClass.class 3.If SubClass.class is not found and SubClass.java is found then it gets compiled(or else the error is shown as Cannot resolve symbol) 4.Then the compilation of SuperClass is starts from next line.
If this is the possible flow then my question is WHILE COMPILING THE SubClass.java WHAT HAPPENES AT THIS LINE? public class SubClass extends SuperClass
for SubClass to extend SuperClass,where does the compiler finds the .Class file for SuperClass( which is not generated yet)? or is it not necessary at compile time?
Originally posted by sarang bharambe: Thats fine guys...
But you all are explaining the flow at runtime and my post asks for the flow during compilation.
I guess the possible flow is:
1.The compilation of SuperClass starts 2.when It Comes to the line SubClass s1=new SubClass(); then the compiler try to find the Class file SubClass.class 3.If SubClass.class is not found and SubClass.java is found then it gets compiled(or else the error is shown as Cannot resolve symbol) 4.Then the compilation of SuperClass is starts from next line.
If this is the possible flow then my question is WHILE COMPILING THE SubClass.java WHAT HAPPENES AT THIS LINE? public class SubClass extends SuperClass
for SubClass to extend SuperClass,where does the compiler finds the .Class file for SuperClass( which is not generated yet)? or is it not necessary at compile time?
I hope someone will respond logically
where are all the wise men gone???
regards sarang
It may be different from compiler to compiler, but the short answer is... The Java compiler is dependent on the fact that the related classes be compiled, it is not dependent of the output of that compilation.
When the compiler realizes that it needs to compile a dependent file, it merely adds it to the list of files that needs to be compiled.