• 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

Method call q

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??



------------------
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic