• 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

How come a super class method have a subclass object?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only thing I would add is don't try this at home or work.

Mark
 
sarang bharambe
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
"
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey
oooops sori yaar.
i was not using Caps S n C there in cmd.
now its fine .

thanx a lot!
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes you are right about the explanation...


Regards
 
sarang bharambe
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic