• 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 override

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I inherit a base class.Base class has one private method.

In the derived class I can override private method.How it is possible private means only particular class has the access ,how a derived class has the access?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are overriding the method from parent class, its not called "accessing" - you are basically modifying the functionality of the base class - completely changing it or enhancing it.
Accessing a private function of the super class would still give compile time error. narrowing down the privilege of the overridden method in child class will also give you compile time error.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jacob deiter wrote:In the derived class I can override private method.


No you can't - in order to override a method, that method must be visible in the overriding class. Therefore, private methods can never be overridden.
Instead, you are simply implementing a new method that happens to have the same signature (name + parameters + return type).
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narrowing down the privilege of the overridden method in child class will also give you compile time error.



can you give some example?

in your reply, you says that private method of base class can be overridden in sub class.
if it so then protected method of base class can not be overridden in sub class ??
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you are simply implementing a new method that happens to have the same signature (name + parameters + return type).



I got the Point
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jacob deiter wrote:

narrowing down the privilege of the overridden method in child class will also give you compile time error.



can you give some example?

in your reply, you says that private method of base class can be overridden in sub class.
if it so then protected method of base class can not be overridden in sub class ??


What amitabh meant is the following:
- a public method must be public in any sub class.
- a protected method must be protected or public in any sub class
- a method with default access must have default access or be protected or public in any sub class

In other words, you can't reduce the visibility.

This is not really related to your initial question though.
 
reply
    Bookmark Topic Watch Topic
  • New Topic