Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private method override

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • 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?
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 584
  • Mark post as helpful
  • send pies
  • 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: 584
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic