Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When designing a class, how do you decide when all overridden methods should call super or when none of the overridden methods should call super? Also, is it considered bad practice if your code logic requires a mixture of supered and non-supered methods like the Javascript example below?

ChildClass = new Class.create(ParentClass,
{
   /**
    * @Override
    */
   initialize: function($super) {
      $super();
      this.foo = 99;
   },

   /**
    * @Override
    */
   methodOne: function($super) {
      $super();
      this.foo++;  
   },


   /**
    * @Override
    */
   methodTwo: function($super) {
      this.foo--;  
   }
});

After delving into the iPhone and Android SDKs, I noticed that super must be called on every overridden method, or else the program will crash because something wouldn't get initialized. When deriving from a template/delegate, none of the methods are supered (obviously). So what exactly are these "je ne sais quoi" qualities that determine whether a all, none, or some overriden methods should call super?

share|improve this question
Stack Overflow? – dbramhall Jun 24 '11 at 20:49
1  
This is an object oriented design question. It's not a technical question. – JoJo Jun 24 '11 at 20:51
My mistake, proceed as you normally would. :) – dbramhall Jun 24 '11 at 21:03
The super must be called on every overriden method or else the program will crash (referring to iPhone and Android SDK and also in context of JavaScript) is not general object oriented design, it's technical. I override without calling 'super' just fine. – Tungano Jun 24 '11 at 23:24

1 Answer

up vote 4 down vote accepted

According to Martin Fowler, this is anti-pattern.

I think it's a good read, if you don't already know that article.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.