0

This question is an extension of the following question: Require override of method to call super

I want all subclasses of my superclass to add to the implementation of their parent's update() method. Since this isn't possible in Java, this is my alternative:

    public class MySuperClass{
        public final void update{
            // common updates for all classes implementing mySuperClass
            updateSub0();
        }

        protected void updateSub0{
            // method for next subclass to override
        }
    }

    public class SubClass0 extends mySuperClass{
        @Override
        protected void updateSub0{
            // common updates for all classes implementing subClass0
        }

        protected void updateSub1{
            // method for next subclass to override
        }
    }

    public class SubClass1 extends SubClass0{
        @Override
        protected void updateSub1{
            // common updates for all classes implementing subClass1
        }

        protected void updateSub0{
            // method for next subclass to override
        }
    }

    ...

Would this method be considered inappropriate? And if so, could you explain?

Any additional suggestion would also be greatly appreciated.

Additional Info

Since I'm asking for suggestions, I'll try to provide some more information. Basically, I'm working on a system where the entire screen is made up of planes (surfaces). Now, every type of plane has a position, a size and a target position (for smooth repositioning), so the Plane class (mySuperClass from the question), would update the position in its update method.

For planes that are press-able by the user, I extend Plane with PlanePressable (SubClass0 from the question). As well as a position, a size, and a target position, PlanePressable's would have an offset field that stores information on how much it has been pressed. So, as well as updating the position, PlanePressable's would need to update offset, hence the updateSub0 method.

Community
  • 1
  • 1
Namgyu Ho
  • 352
  • 4
  • 12
  • If you want/need that any proper subclass has to implement the `updateSub0` method, mark it as `abstract` in the super class. – Luiggi Mendoza Aug 06 '15 at 14:53
  • 1
    Actually, all classes below `mySuperClass` is meant to be non-abstract (instantiate-able). – Namgyu Ho Aug 06 '15 at 14:55
  • You can inherit in java by using the keyword 'extends' – hermit Aug 06 '15 at 14:57
  • @pramithasdhakal Oops, second stupid mistake. I meant for all of the classes to extend the ones above. – Namgyu Ho Aug 06 '15 at 14:59
  • My point is that you need to make your super class abstract, not the sub classes. In that way, you will force each subclass to always implement this method. I never talked about the sub classes in my comment. – Luiggi Mendoza Aug 06 '15 at 15:00
  • Also, this is a really bad design. It will be better to use decorator pattern rather than this. – Luiggi Mendoza Aug 06 '15 at 15:01
  • @LuiggiMendoza I see. Well in that case, do you have any suggestions on how to force further subclasses (`SubClass1`, `SubClass2`...) to implement the `updateSub#` methods? – Namgyu Ho Aug 06 '15 at 15:06
  • @user3713433 by using the `extends` keyword, like we've explained to you. And by making your base class abstract like Luiggi explained. – Lawrence Aiello Aug 06 '15 at 15:09
  • @LuiggiMendoza Okay, I'll check out the Decorator pattern and get right back to you. – Namgyu Ho Aug 06 '15 at 15:10
  • My suggestion is: forget about `updateSub#` method, do some research about [decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern) and try to implement it. Some examples of it can be found here: https://github.com/iluwatar/java-design-patterns/tree/master/decorator – Luiggi Mendoza Aug 06 '15 at 15:10
  • @LuiggiMendoza I checked out the Decorator pattern and it doesn't seem to fit what I'm trying to do here. I added some additional info about my situation in the original post. Hope you can check it out. – Namgyu Ho Aug 06 '15 at 15:38
  • From the code example I've provided, it suits your case perfectly. You can have a `Plane` with the proper fields and a `PressablePlane` that wraps `Plane` reference and in the `update` method performs `doSomething(); plane.update(); doSomethingElse();`. In your case, if you don't need to add functionality to the `update` method like counting ticks, then this design won't make it either. – Luiggi Mendoza Aug 06 '15 at 15:41
  • @LuiggiMendoza Oh, I see what you're talking about. It would work perfectly! Although, should I be concerned about what the name 'Decorator' implies? I feel that the Planes really do *inherit* from their parents rather than *decorate* them. – Namgyu Ho Aug 06 '15 at 15:54
  • Decorator just means to decorate behavior. It is, wrapping an object and adding behavior dynamically regarding what this object needs. From the current description of your problem, seems like you don't even need it because a plane can be updated in the view and updated when the user clicks on it, which are two different behaviors and one should not mess with the other. – Luiggi Mendoza Aug 06 '15 at 16:05
  • @LuiggiMendoza When it comes to `PressablePlane` there's no problem but I'll also have subclasses of `PressablePlane` with additional update requirements. I'll also have a subclass of `Plane` called `PlaneCollection` which is Plane that includes multiple Planes. These `PlaneCollection`s need to manage the Planes they contain during their updates. About your second point, the way I'm handling updates is, `update` method is invoked from a `PlaneManager` 60 times per second (using a `Thread`), and all information is passed onto the Planes themselves to handle locally. – Namgyu Ho Aug 07 '15 at 04:22

1 Answers1

0

You have to use interitance and the super keyword like so:

public class subClass0 extends mySuperClass {
    @Override
    protected void updateSub0{
        super.update() // To call parent's update method
        // common updates for all classes implementing subClass0
    }

    protected void updateSub1{
        // method for next subclass to override
    }
}
Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35