• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

How to access the overriding variable?  RSS feed

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a post a while back asking how to call the overridden method. The answer that I got is that you cannot. One good argument given was that of polymorphism, that you are constructing the subclass, so it is the subclass' method (and not the superclass) that should be called.
But when it comes to a hidden variable, it is different. If the method that uses the variable is in the superclass, even though that method is inherited, it is the superclass variable that is used.

Result:
A.m1() i:10
B.m2() i:20
If I were to use the same argument used for overridden methods then the program should constantly use B's i variable. The program is constructing a B object, not A. But that is not what is happening here.
Because of this behaviour, if I want to reuse A's m1(), but using my own variable, I cannot. I have to override m1() with the same set of codes.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...I want to reuse A's m1(), but using my own variable...



I think the TEMPLATE METHOD Pattern can be applied to your problem. A class implements an algorithm. The algorithm is broken down into primitive operations. A subclass implements the primitive operations.
In your example, the primitive operation is merely to read a variable i.
[ June 28, 2003: Message edited by: Marlene Miller ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!