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;
}
}
3
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.
0