-1

Suppose i have an abstract base class Parent which defines an abstract Method A(some parameter) taking a parameter, also it defines an instance Method B which calls method A(parameter) inside its body and passing some parameter.Then we have an concrete child class B which extends the base class and provide its own implementation of Method A using the parameter passes. I have seen many implementation of this practice in frameworks. I want to know how it works and does it provide any benefits.

Here is some code written in Android

public abstract class ParentActivity extends Activity {
    public abstract void onResume(LoginToken token);

    @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               Some implementation code
        }

    @Override
        public void onResume() {
              super.onResume();
              Some implementation code
              onResume(token);
        }
}

public class ChildActivity extends ParentActivity{
        LoginToken token;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               Some implementation code
        }

    @Override
        public void onResume() {
              Some implementation code
              super.onResume();
        }

    @Override
        public void onResume(LoginToken token) {
             this.token=token;
        }
}
  • Did you learn what abstract and virtual methods are and how they work? – Euphoric Apr 6 '14 at 6:50
  • ya, but i want to know how it is possible to call the abstract method before the method itself has been implemented. – anmrk Apr 6 '14 at 7:11
  • 3
    Thats why I said to learn about abstract and virtual methods. They explain it properly. – Euphoric Apr 6 '14 at 7:29
0

What you're describing is the Template Method Pattern. The benefit is to describe an overall process (like log in, do something, log out) while the actual steps (or some of them) are implemented by concrete sub-classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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