0

I am trying to create a sprite parent class that uses some of it methods to filter out some of the dud conditions by returning null prior to moving onto the child method. I think this is referred to as a 'bouncer pattern' when used in a single method but couldn't find any examples that extend across parent / child objects.

Below is an basic example of the code;

class parent ()
{
    public void update()
    {
         if (this.count<0)
         {
             return;
         }
         // do parent stuff
    }
}
class child extends parent ()
{
    public void update()
    {
         super.update()
         //do child stuff here
    }
}

When i run the child update method i would like it to run the parent method and if the count is less than 0 just stop an not bother with the parent and child stuff.

What i am finding is that once the count is less than 0 the parent stuff stops but the child stuff keeps going. It is like both of the update methods are being treated as separate functions.

I believe i need a way to append the child update method to the child update method. I am hoping there is a simple way of achieving this but will accept complicated solutions as long as i don't have to write the bouncer/filter code in every child class.

Any help is much appreciated.

Jason

#

Alternative solution

Proposed code class parent () { public void update() { if (this.count<0) { return; } // do parent stuff child_update()

    }
    child_update()
    {
         //place hold to be overridden by child
    }
}
class child extends parent ()
{
    @Override
    private void child_update()
    {
         //do child stuff here
    }
}

I think this solves my problem but haven't had chance to test it yet. When i call the child object update method it should be directed to the parent method, which performs the bouncer/validation checks and if applicable runs the child update that has been overridden by the child method.The child method would be set to private so it can't be called without the validation checks.

I personally think this is a cleaner solution than the boolean solution being proposed but would like to know what other people think.

thanks

Jason

jklmuk
  • 5
  • 3

2 Answers2

1

Unless there's condition in your child's update() that prevents this from happening, this all works as expected. By callingsuper.update() does NOT replace child's update(). It just calls it like it would be any other method. And the only difference here is that you particularly want to call your parent's method your child extends, hence super.. But once parent's update() returns, the remaining code of caller is executed.

You shall change your update() to i.e. return boolean when method code was executed, and then make your child's code look like:

@Override
public boolean update() {
  if( super.update() ) {
       ...

     return true;
  }

  return false;
}

Also note use of @Override (see here to find out what it is)

Community
  • 1
  • 1
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Sorry it won't let me edit my previous comment. I had thought about using a return type of boolean. My main issue with this that i would have to include additional functions within the child methods. As two of you have posted the same solution it looks like it is the way to go and is quite simple to implement. However, i thought up an alternative solution which doesn't use a return boolean and will edit the question shortly to include an basic example of what i think works. Please let me know what you think and which way you would recommend. – jklmuk Jul 17 '15 at 20:59
0

Couldn't you just make the return type of the method `boolean' and check it in the child class.

    class parent ()
    {
        public boolean update()
        {
             if (this.count<0)
             {
                 return false;
             }
             // do parent stuff
             return true; 
        }
    }
    class child extends parent ()
    {
        @Override
        public boolean update()
        {
             if( super.update() )
             {
                 // do child stuff
             }
            return true;    


  }
}
CWhitten
  • 91
  • 3
  • Hi thanks for replying. I had thought about using a return type of boolean. My main issue with this that i would have to include additional functions within the child methods. As two of you have posted the same solution it looks like it is the way to go and is quite simple to implement. However, i thought up an alternative solution which doesn't use a return boolean and will edit the question shortly to include an basic example of what i think works. Please let me know what you think and which way you would recommend. – jklmuk Jul 17 '15 at 20:58