I have an abstract method that implemented by other classes:
protected abstract void uninstallApp();
What I want to do is to force all the classes that must implement this method to use System.out.println()
and another method: Log.report()
Is there any way that I can achieve this?
You could for example, do your logging stuff in the parent abstract class in a final method (so the child will not overwrite it). This class call a second method that the child classes should overwrite. Something like following:
protected final void uninstallApp(){
doUninstallApp();
Log.report();
}
protected abstract void doUninstallApp();
I agree with @Loic, we can go here for Command pattern...I tried to write the code below..May be It can help..
class Parent {
public final void uninstallApp() {
print();
doSomethingUsefull();
logReport();
}
//To be overriden as per requirement
protected void logReport() {
Log.report();
}
//To be overriden as per requirement
protected void print() {
System.out.println();
}
//To be overriden as per requirement
protected void doSomethingUsefull() {
//Implementation goes here
}
}
class Child extends Parent {
public void logReport() {
//Implementation goes here
}
public void print() {
//Implementation goes here
}
public void doSomethingUsefull() {
//Implementation goes here
}
}
class Main {
public static void main(String[] args) {
Parent p = new Child();
p.uninstallApp();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.