• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I guarantee that a superclasses' method is called?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for checking this out =]

What I would like to know is, if I have a superclass: A and a subclass B with a particular method in A and that method is overriden in B.
Lets say that A has a certain function...maybe to paint text. How can I guarantee that this function is always called, and that it cannot be changed in
the overriden classes?

Many thanks
 
Ranch Hand
Posts: 140
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside your overridden method in your subclass B, call "super.methodName()", this will call the method in your super class. Now when you call your method in the subclass it will always execute the method in your super class.

 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks very much for your response. However, what I meant to say but I forgot was: Assume that I want this method to be executed ALWAYS, AND, assume that someone else has access to my subclasses. They will override my method, but how can I still guarantee that a particular command called in the superclass' method is called, regardless if they change the implementation in the subclass.

Cheers!
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keshan,

What you're looking for here is the template design pattern. You would like the super always to be executed and the additional subclass specific steps to be executed. It's actually an anti-pattern to force the user to call super.methodName() when you don't need to for the problems you are seeing. If this is a required method, you don't want to leave it up to the user to forget (accidentally or maliciously). Some sample code is below. A client will call methodName() which will call the SuperClassA implementation and then methodNameImpl() which is implemented by SubClassB.



Note: If you want SuperClassA to be able to be instantiated directly, you should remove the abstract and give methodNameImpl an empty implementation in SuperClassA. Then no extra behavior will be executed if you use SuperClassA, and optionally, subclasses can override for specific additional behavior.

Hope this helps,
Jeff
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. Sorry. If you tell us what problem you are trying to solve, perhaps we can think of another solution.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would go with Jeff's solution. In fact, I have gone with that solution several times myself
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic