Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

concept of method overriding

 
Sandeep Tyagi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we can't use narrowing method in sub class on the case of method overriding
Thanks in advance

with Regards
Sandeep Tyagi
sandeep_pura@yahoo.com
+919811903737
0124-4382173
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it is because of polymorphism...

say you have a ClassA, with a public doSomething() method.

now, ClassB extends ClassA. say you could make B's doSomething() private.

now you make a collection of ClassA. you get a ClassB object, which you can put into the collection, since a ClassB is a ClassA.

now you iterate through the collection, calling doSomething() on every object. when you get to that ClassB, you're gonna have a problem...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic