• 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

Inheritance question

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I have created two classes:

I want class bar's function hello to execute its parents code but I don't want to call super.hello() to do it. I need this code to be executed on its own without any intervention.
Basically I want to subclass class foo and add functionality to the method hello and have it execute its parents code then executes its code. But I don't want to add super.hello() to each subclassed foo class.
Does this make sense?
Frank
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... an unusual question. The only reason I can think to do this is if you don't trust the authors of overriding methods to write their code correctly. (Which I think may be a healthy level of paranoia in many work environments.) If that's the case, here's what I might do:
<code><pre>class foo {
public foo() {}
public final void hello() {
System.out.println("Hello ");
hello2();
}
protected void hello2() {}
}

class bar extends foo {
public bar() { }
protected void hello2() {
System.out.println("World");
}
}</pre></code>
By making hello() final in foo, we can force any subclasses to override hello2() instead. Anyone calling hello() on an object derived from foo will execute the foo.hello() method, and then whatever hello2() has been overridden to. Of course, your co-workers can still get around this by executing hello2() directly if they wish - but note that hello2() is protected while hello() is public. Any other classes not inheriting from foo (and outside the current package) will only be able to use hello(), which I gather is what you want.
If you do something like this, you should probably make it clear in the javadoc comments that subclasses should override hello2() in order to add functionality to hello(). Otherwise your co-workers may just decide you don't have any idea what you're doing. Of course, your co-workers may not read the javadoc comments (which is probably why you don't trust them to override your code correctly) but at least this way you can claim the moral high groud. "What do you mean my code doesn't work? Didn't you read the documentation I thoughtfully provided?"
 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me explain why I might need to do this. I say might because I may be totally confused at this point.
Okay here goes.
I've got a class which handles an action event for a custom component in my Java/Swing program. In order for the user/programmers using my custom component to add an action event to it they need to subclass DefaultSelectAction class and add there functionality. Now I may have this totally wrong but I need the DefaultSelectAction class to have its own functionality that gets executed for each action. I don't want a programmer using my API to have to call super for the actionPerformed() method and the constructor of the DefaultSelectAction class. I want them to add there action code and thats it.

Now at the moment in order for a programmer to add an action to this component he has to do the following which is very ugly.

Hopefully this makes my problem clear. Hopefully there is a logical explanation to this. Of course I may be doing it totally wrong too!
Hope you can help!
Frank
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic