• Post Reply Bookmark Topic Watch Topic
  • New Topic

K&B p102 not clear  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

"A subclass within the same package as the instance's superclass can override
any superclass method that is not marked private or final. A subclass in a
different package can override only those non-final methods marked public
or protected"

I tried both the cases - created a subclass in a package and tried to override the private method of it's superclass. final is clear, you cannot override a final method. I also tried to override the private method in a subclass in a different package and it worked too. Am I missing something here? Could somebody pls explain?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't really override the method, what you did was define a new private method that has the same name as the super class's private method. For example:





If you compile and run this code with the private modifier on the doItPrivately() method in Foo, the method is not overridden in the subclass, and the subclass version will not be called. However if you comment out the private modifier and uncomment the public modifier, you will see the polymorphism in action.
[ January 22, 2007: Message edited by: Garrett Rowe ]
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I got it now. Thanks a lot!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!