• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private method overiding

 
Chrix Wu
Ranch Hand
Posts: 121
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your reference type is of Base class. So it would invoke the Base class method.
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic