GeeCON Prague 2014*
The moose likes Java in General and the fly likes private method overiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "private method overiding" Watch "private method overiding" New topic
Author

private method overiding

Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121


I read <thinking in java>, the output of this code is "private f()"
the book does not explain well.

my understanding is :
derived class and base class has identical f() method, but derived one does not override base one.
so when po.f() is called, it should still goes to the derived class' method ,
which should output "public f()"
BUT why is that incorrect?


** SCJP 5.0 84% **
** SCWCD 1.5 76% **
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Your reference type is of Base class. So it would invoke the Base class method.


Mohamed Sanaulla | My Blog
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

when you use private modifier to method/variable then it is visible only in that class in which they are declared. In your case you have method private void f() in Parent class. This method is not visible to Subclass. If Subclass doesn't even know that void f() method is exist. Then How it will ovverride this method? Therefore when you run this program then Parent class method is invoked.

If suppose you fave another method as public void f1() as



Now subclass version of method is invoked and you get output as



Life is easy because we write the source code.....
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Mohamed Sanaulla wrote:Your reference type is of Base class. So it would invoke the Base class method.

what about dynamic binding, in runtime, the app should execuete the acutal object (not reference object)'s method, which is 'public f()'
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Chrix Wu wrote:
Mohamed Sanaulla wrote:Your reference type is of Base class. So it would invoke the Base class method.

what about dynamic binding, in runtime, the app should execuete the acutal object (not reference object)'s method, which is 'public f()'


But that is only when the method is being overridden. In this case the f() in the Derived class is some method totally not related to the one in Base class.
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Mohamed Sanaulla wrote:
Chrix Wu wrote:
Mohamed Sanaulla wrote:Your reference type is of Base class. So it would invoke the Base class method.

what about dynamic binding, in runtime, the app should execuete the acutal object (not reference object)'s method, which is 'public f()'


But that is only when the method is being overridden. In this case the f() in the Derived class is some method totally not related to the one in Base class.


OK, i think i get it...

The logic is as followed:

no overriding -> no dynamic binding -> only calls the reference type's method rather than the actual object's method

correct?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Chrix Wu wrote:no overriding -> no dynamic binding -> only calls the reference type's method rather than the actual object's method

correct?

Yes. Private methods cannot be overridden.

Try making the private method f() in the superclass protected and see what happens.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

private methods are final automatically . the method is invisible in subclass . that is why it is always better to use a different method name from a private base-class method in your derived class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Seetharaman Venkatasamy wrote:private methods are final automatically

You could say that, but it's not completely accurate. Inheritance and overriding just doesn't apply to private methods, so the concept of "final" doesn't apply to private methods.
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

Jesper de Jong wrote:
Chrix Wu wrote:no overriding -> no dynamic binding -> only calls the reference type's method rather than the actual object's method

correct?

Yes. Private methods cannot be overridden.

Try making the private method f() in the superclass protected and see what happens.



How can CLASS be PROTECTED ??
class can be only default or public ...
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

Jesper de Jong wrote:
Chrix Wu wrote:no overriding -> no dynamic binding -> only calls the reference type's method rather than the actual object's method

correct?

Yes. Private methods cannot be overridden.

Try making the private method f() in the superclass protected and see what happens.


this is wrong

overidding -> depends on object type
overloadind -> depends on reference type
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

munjal upadhyay wrote:
Jesper de Jong wrote:Try making the private method f() in the superclass protected and see what happens.

How can CLASS be PROTECTED ??
class can be only default or public ...

I wrote: Try making the method f() in the superclass protected, I did not say anything about making a class protected.

And it's not wrong: you cannot override private methods, because private methods are not inherited from superclasses to subclasses.
Vincent Edomwonyi
Greenhorn

Joined: Jan 19, 2011
Posts: 8
The general rule here is this, when you use a super class as a reference type and subtype as the object, you can only invoke methods that are COMMON to both classes. If the method was overridden by the subtype then at run time the subtype method is the one that runs.

Sub Classes have NO KNOWLEDGE of private methods in their super classes. If both classes have no methods common to them, then the super type reference invokes the method in it's own class. (The Devil you know is better than the Angel you don't know!)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

munjal upadhyay wrote:How can CLASS be PROTECTED ??
class can be only default or public ...

That only applies to top-level classes. This compiles just fine:




"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try to add the @Override annotation to the method f() in Derived. Then the compiler will complain about that it is not actually overriding anything.
 
wood burning stoves
 
subject: private method overiding