I have class Dad with subclass Son. I’d like to create a subclass of Dad and a subclass of Son that overrides a method of Dad.
What would be the best way of doing this without repeating code? I can not modify Dad and Son.
Given…
public class Dad {
public void doSomething() {}
}
public class Son extends Dad {
}
…I’d like to create…
public class DadSubclass extends Dad {
@Overrides
public void doSomething() {
// My code
}
}
public class SonSubclass extends Son {
@Overrides
public void doSomething() {
// My code
}
}
…without repeating // My code.
The obvious solution would be to create a helper class and call it for both, but this is problematic if I want to call protected methods, and I’m not allowed to create the subclasses with the same package.
Is there a better solution?
Create a common helper class and call it.