Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
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?


"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
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 ]

Regards,<br />Seb<br /> <br />SCJP 1.4
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
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

Joined: Sep 19, 2005
Posts: 100
Hi Seb and Ritu,

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

Thanks to both of you!
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
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??


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
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

Joined: Jun 22, 2005
Posts: 1526
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

Joined: Sep 28, 2005
Posts: 225
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

Joined: Sep 28, 2005
Posts: 225
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

Joined: Jun 22, 2005
Posts: 1526
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.

http://www.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

Joined: Sep 28, 2005
Posts: 225
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

Joined: Oct 03, 2004
Posts: 102
Hi Seb,

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

Rgds,
Ritu
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
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

Joined: Sep 28, 2005
Posts: 225
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

Joined: Jun 22, 2005
Posts: 1526
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

Joined: Oct 03, 2004
Posts: 102
Thanks Seb!
itisha jain
Greenhorn

Joined: Oct 04, 2005
Posts: 11
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

Joined: Sep 28, 2005
Posts: 225
The code of Bird & Raptor is an exemple of method overloading.

The explantion can be found here
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding
 
Similar Threads
Overriding Doubt
MindQ #35
MindQ's question no. 35
object resolution problem
explain