• 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

private method overiding

 
Ranch Hand
Posts: 121
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reference type is of Base class. So it would invoke the Base class method.
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!)
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic