File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How come a super class method have a subclass object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How come a super class method have a subclass object?" Watch "How come a super class method have a subclass object?" New topic
Author

How come a super class method have a subclass object?

sarang bharambe
Ranch Hand

Joined: Apr 01, 2005
Posts: 40
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
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...


...ToucH SkieS
AKASH
SCJP 1.4 86%
SCWCD 1.4 82%
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

The only thing I would add is don't try this at home or work.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sarang bharambe
Ranch Hand

Joined: Apr 01, 2005
Posts: 40
hey guys,

Thanks for the response ....

but I am still not convinced

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
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
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
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
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
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: 979
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
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: 979
Hi,

Yes you are right about the explanation...


Regards
sarang bharambe
Ranch Hand

Joined: Apr 01, 2005
Posts: 40
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How come a super class method have a subclass object?