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

Regarding overriding

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we are overriding a method,the overridden method should not have access specifier narrower that the overriding method.Please tell me the reason.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object of a subclass must always be able to be used as if it were an object of its superclass. Tightening access modifiers violates this principle.

The principle is called [some bloke whose name begins with L and ends OV]'s Substitution Principle, and is one of the basic rules of object orientation.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
some bloke whose name begins with L and ends OV


It appears I am an appalling sexist. It's a woman: Barbara Liskov.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Please let me know how tightening violates the principle and which principle exactly it violates.If possible,please explain through some example.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this sounds like it could be homework, I don't feel inclined to provide a full answer that could just be copied. I think you should do some Googling first. I've given some very good clues.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
It is a bit urgent.Please tell me which principle.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got it .It is Liskov substitution Principle.
Thanks
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got it .It is Liskov substitution Principle.
Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of like buying a AA battery for my TV remote. No matter who makes it, I expect it to fit my remote. I'd be really ticked if I bought batteries and found out they were too short to fit.

Think about shopping for a List. List is an interface that might be implemented by ArrayList, LinkedList or some new fangled List that I write. When you buy a List you know you can call size() to see how many things are in the list. What if I make my size() method private? I advertise "This is a List!" on the box, but you get it home and there's no size() method! That would be so bad that the compiler just won't let me reduce the visibility.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic