• 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

Overriding

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm just confused about this program:

class Super {
int a = f();
int f() {
return 1;
}
}

class Sub extends Super{
int b = 2;
int f() {
return b;
}
}

public class ClassHierarchy {
public static void main(String[] args) {
Super sup = new Sub();
System.out.print(sup.a);
System.out.print(sup.f());
}
}

I thought the output here is "22" but the real output when run is "02". Where did the "0" came from?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a Sub instance, Super is first initialized, here's the flow :
1. new Sub()
2. Super instance init : a = f()
3. f() is overriden in Sub -> return b
4. AT THIS POINT b IS NOT INITIALIZED : b=0
5. a=0
6. Sub variables init : b=2
7. So sup.a = 0
[ October 04, 2005: Message edited by: Seb Mathe ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chard,

Your Code:
-------------------------------------------------------
class Super {
int a = f();//1
int f() {
return 1;
}
}

class Sub extends Super{
int b = 2;
int f() {
return b;
}
}

public class ClassHierarchy {
public static void main(String[] args) {
Super sup = new Sub();
System.out.print(sup.a);
System.out.print(sup.f());
}
}
--------------------------------------------------------------

Remember few things:
1. instance variables are resolved at compile time.
2. overridden methods are resolved at run time.

Now when you Instantiate an object variable are assigned default values. Methods executes during runtime. So during compilation time, variable a is assigned a default value 0 and hence on displaying sup.a, you get 0. but, as stated above, methods are resolved at runtime, when you write following statement: 'System.out.print(sup.f());' a 'Sub' class method gets called and you get the output 2, and thats why you are getting output 02.

Hope this will answer your query.

Thanks,
Ritu
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seb and Ritu,

Both your explanations has helped me a lot realizing things that happended in the code.

Thanks to both of you!
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ritu Kapoor:
...So during compilation time , variable a is assigned a default value 0 and hence on displaying sup.a, you get 0. [/QB]



Are you talking about compile time or run time??

a gets a 0

that is visible, but I want to know if it gets this "0" through automatic-initialization or by calling f() in sub as Seb has put it??
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akhil,

I wrongly wrote it during compilation time instead of ' during automatic-initialisation process'

Thanks for correcting me.

Rgds,
Ritu
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its ok Ritu.

I am waiting for Seb's response.

Originally posted by Seb Mathe:

1. new Sub()
2. Super instance init : a = f()
3. f() is found in B -> return b



1. is ok...
2... is ok...
3... Hold on here Seb

I have these queries...

1. Can a super class call its own child's method??
2. Is there anything like if a method is not available in the parent class it would search them in the child classes??? How does the parent class know that it has so and so number of children???
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Akhil,


1. Can a super class call its own child's method??
2. Is there anything like if a method is not available in the parent class it would search them in the child classes??? How does the parent class know that it has so and so number of children???



Sorry if I was'nt very clear in my post : the initialization of variable a is done by calling method f() on the instance. We're talking about instances, not classes. So we're calling f() on the object created by Super sup = new Sub(); which is an instance of Sub, which has an f() method, which overrides f() in Super. So calling f() on variable sup will return sup.b which at this point of the flow is not inizialized, so we're getting default value from b : 0.

I think that the important thing is to remember we're dealing with instances, not with classes, it's of course different within a static context.

Executing the code step by step with a debugger helps to unsderstand the process.

Hope I've answered your question
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops Ritu, I've just read your post, and think you're wrong here :

variable a is assigned a default value 0 and hence on displaying sup.a, you get 0



When System.out.ptintln(sup.a) is called, a has been assigned to the return of the call to f()...
If you replace Super sup = new Sub(); by Super sup = new Super(); you'll get 11, no ?
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seb Mathe:

When System.out.ptintln(sup.a) is called, a has been assigned to the return of the call to f()...



Two queries Seb....

Number 1:

"a is assigned return of the call to f()"... which f()?? the one in Super itself or the one in Sub??
If it is call to f() method from sub() then what is the REASON behind??

Number 2:
System.out.print(sup.f())

I have been screaming here again and again, that

You can have a child object in a parent reference. Using such a reference to access the method, would search the called method in parent class only.


https://coderanch.com/t/251032/java-programmer-SCJP/certification/Overriding-Vs-Shadowing

Am I making any blunder here?? Please correct me seb where I am wrong...
[ October 04, 2005: Message edited by: Akhil Trivedi ]
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
English is not my mother tongue, sometimes I've some troubles for writing what I mean...

1. The f() method of object sup, which is hold in a variable of type Super, but is an instance of Sub. f is overriden in Sub, so f in Sub is called.

2. I don't agree, except for static methods. That's overriding, not Overloading
Try to change the code like that :



It compiles fine... And result would be the same.

We can find another examples of children objects stored in variables of their Parent's type, and which overrides some methods.

See the method toString() of class Object...
The code below :

prints "hello" and not java.lang.String@XXXXXX(which will be the output if toString in class Object was called).
[ October 04, 2005: Message edited by: Seb Mathe ]
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seb,

Then what do you what is the correct explanation for this query?

Rgds,
Ritu
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try again...

System.out.println(sup.a) prints 0
Because :
- a is initialized before b (calling new Sub(), initialization is done downward the class hierarchy)
- f() definition of class Sub is used because f in class Sub is overriding f in class Super and we're dealing with an instance of Sub.

System.out.println(sup.f()) prints 2
Because :
- after the initilization process b equals 2 and
- f() definition of class Sub is used because f in class Sub is overriding f in class Super and we're dealing with an instance of Sub.
[ October 04, 2005: Message edited by: Seb Mathe ]
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akhil, I've found this code in a thread you've cited in one of your post :


This is an example of method overloading. And for this case, I agree with you whan you say

You can have a child object in a parent reference. Using such a reference to access the method, would search the called method in parent class only.



Overriding is just another thing...
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seb, by the way English is not my mother tongue too.

I know what is overloading... In fact my statement...


You can have a child object in a parent reference. Using such a reference to access the method, would search the called method in parent class only.


is still right (but I do agree it needs some modification somewhere) can be supported on the basis of following code...



I modified the code and now it goes like this....

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seb!
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seb,

I went thru this code of bird n raptor.Please explain ,when we pass an object instance in print() why does it call the parent's method though when we give no args it calls the child method,as expected.
Please tell when will the parent's method be called?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code of Bird & Raptor is an exemple of method overloading.

The explantion can be found here
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here goes my modified quote.


You can have a child object in a parent reference. Using such a reference to access the method, would FIRST search the called method in parent class.
1. If the function doesnt exist in parent class, its a compile time error.
2. If the function exists in parent class and is overridden in child class, then based on "object type" child's overridden copy is called.



Thanks a lot seb.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic