wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method call q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method call q" Watch "Method call q" New topic
Author

Method call q

kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
class Parent{

2) private void method(){

3) System.out.println("Parent.method invoked");

4) }

5) public static void main(String args[]){

6) Parent p = new Parent();

7) Parent c = new Child();

8) p.method();

9) c.method();

10) }

11) }

12)

13) class Child extends Parent{

14) public int method(){

15) System.out.println("Child.method invoked");

16) return 0;

17) }

18) }

OUtput??
william shen
Ranch Hand

Joined: Jun 07, 2001
Posts: 60
I think a compile error will occur,because the child class have a same named method but different return type. If i am wrong,ple let me know,thanx!
Originally posted by kevin goon:
class Parent{

2) private void method(){

3) System.out.println("Parent.method invoked");

4) }

5) public static void main(String args[]){

6) Parent p = new Parent();

7) Parent c = new Child();

8) p.method();

9) c.method();

10) }

11) }

12)

13) class Child extends Parent{

14) public int method(){

15) System.out.println("Child.method invoked");

16) return 0;

17) }

18) }

OUtput??


------------------
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
Output should be:
Parent.method invoked
Parent.method invoked
Explanation:
Since the method() in the Parent class is private, it's invisible to the Child class. Therefore it's perfectly O.K. to define a method with the same name and parameter in Child class with different return type. In such case we say that the method() in Parent class is shadowed by the method() in Child class just like private fields.
As such, whever you call p.method() or c.method(), the compiler only looks at the class (i.e. Parent) of the variable it denotes, instead of the actual object it refers.
kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
Thanks for the explanation. I think your answers are right.
ANother relating question i have is,
What if the Child class method had the same return type(void) as the Parent class method? Would it also work?
Also, Is it always true, if
Parent p = new Child();
p.method(); // assume method is defined both Parent, and Child
^this will always call the method in the "Parent" class?? Is it because we consider the reference only?? and not the object it's pointng to???
Please explain this, as I had previously thoght we look at the actual data pointed by the reference when executing during runtime.
thank you=)
william shen
Ranch Hand

Joined: Jun 07, 2001
Posts: 60
A very good question,i am wrong. Let me have deeper understanding about override. Thank you !
Originally posted by Kevin Yip:
Output should be:
Parent.method invoked
Parent.method invoked
Explanation:
Since the method() in the Parent class is private, it's invisible to the Child class. Therefore it's perfectly O.K. to define a method with the same name and parameter in Child class with different return type. In such case we say that the method() in Parent class is shadowed by the method() in Child class just like private fields.
As such, whever you call p.method() or c.method(), the compiler only looks at the class (i.e. Parent) of the variable it denotes, instead of the actual object it refers.


------------------
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
Hi kevin,
Snce the method() in the Parent class is private & is invisible to the Child class, of course it's O.K. to have void or whatever return type in the Child class method(). (Provided that in this case you change the "return 0;" statement to "return;" or just delete it.)
If you use:

the output will still be the same. If you look carefully, you've just used another name to create the similar instance: c instead of p:

so the two lines output will be the same.
You're right in that normally we look at the actual object pointed by the reference when executing methods during runtime because of dynamic binding. We only look at the reference when evaluating member variables because variables are always shadowed. But this case is special in that you use a private method. By making it private, it becomes invisible (or susceptible to be shadowed by subclasses) therefore it acts like the case for the variables.
Hope this is clear enough.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Method call q
 
Similar Threads
same method inheritance
run time error
Inheritance
Static methods and inheritance.
Overriding/overloading