Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:36:26+00:00 2026-06-17T17:36:26+00:00

Here’s the scenario -> imagine there are 3 classes, i want to do something

  • 0

Here’s the scenario -> imagine there are 3 classes, i want to do something kind of like:

public class GameObject {
    public void updateBounds() {
        // do something
    }
}

public abstract class Enemy extends GameObject {
    public abstract void updatePosition(){ //<-- this will not compile, 
                                    //but this is what i want to do, to force 
                                    //child to override parent method
        updateBounds();
    }

}

public class Minion extends Enemy {
    @Override
    public void updatePosition() {
        super.updatePosition(); // <-- how do i throw an exception if this line
                                // is not called within this method of the
                                // child?
        // now do something extra that only Minion knows how to do
    }
}
  • how do you design the Enemy class so that it has a method which does something, but requires every child to override it?
  • how do you force the child (who had to override the method) to also call the parent’s method?

this is almost like Android’s Activity class that has the onCreate, onStart, onResume…etc. methods that are optional, but if you make use of it, it forces you to call super. it can’t be abstract because i want some code to be run when the method is called (which is only in the parent class’s method). bonus points if u know how they did it this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:36:27+00:00Added an answer on June 17, 2026 at 5:36 pm

    The Android Sources use a boolean called mCalled that is set to true inside of the quasi-abstract method implementation. In your case that would be inside of the original updatePosition().

    Then when you want to call updatePosition(), call it through this:

    private void performUpdatePosition() {
        mCalled = false;
        updatePosition();
        if (!mCalled) throw new SuperNotCalledException();
    }
    

    and updatePosition() would look like this

    protected void updatePosition() {
        mCalled = true;
        updateBounds();
    }
    

    EDIT:

    Now that I think about it, the way android does it is a little round about. Because all calls to updatePosition() are going through performUpdatePosition(), you no longer need to have some code inside updatePosition() that can be overridden, but shouldn’t.

    A much better approach is to simply move the required actions to performUpdatePosition():

    private void performUpdatePosition() {
        updateBounds();
        updatePosition();
    }
    
    protected void updatePosition() {
        //Do nothing by default
    }
    

    This way the callee doesn’t have to worry about calling super.updatePosition. If the subclass doesn’t override that function, then nothing extra will happen, whereas if they do, the override will add on to the previous behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here's some sample HTML <style> .icon {display:none;} </style> <ul> <li>ABC <i id=abc class=icon>x</i></li> <li>DEF
Here is my AsyncTask. Getting exception in the AsyncTask.. private class GetCategories extends AsyncTask<String,
here Html given below <div class=navBulletsWrapper> <div rel=0 class=></div> <div rel=1 class=></div> <div rel=2
Here are the yammer api docs: http://developer.yammer.com/restapi/ Am I missing something here? How can
Here's my situation. I have a DotNetNuke application. I want to link to an
Here is a scenario: User installs .NET application that you have made. After some
Here is the code I have. This is my PrintToFile class import java.util.*; import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.