Hello,
I have an abstract class Base which implements interface If1.
Classes D1, D2 and D3 are derived from Base.
Right now, the "common" methods from If1 are implemented only in the Base class and the specialized methods are implemented directly in the derived classes. The "common" methods are not implemented in any way in the derived classes (they are just inherited). For example:
interface If1 {
void common_m1();
void m2();
}
public abstract class Base Implements If1 {
public void common_m1() {
// implementation
}
}
public class D1 extends Base {
public void m2() {
// D1 specific implementation
}
}
public class D2 extends Base {
public void m2() {
// D2 specific implementation
}
}
public class D3 extends Base {
public void m2() {
// D3 specific implementation
}
}
There is a proposition to implement all the methods in the Base class and to override all the methods in the derived classes. For common methods, the method from the derived class will use super to call the base class implementation, like this:
interface If1 {
void common_m1();
void m2();
}
public abstract class Base Implements If1 {
public void common_m1() {
// implementation
}
public void m2() {
// dummy implementation
}
}
public class D1 extends Base {
public void common_m1() {
super.common_m1();
}
public void m2() {
// D1 specific implementation
}
}
public class D2 extends Base {
public void common_m1() {
super.common_m1();
}
public void m2() {
// D2 specific implementation
}
}
public class D3 extends Base {
public void common_m1() {
super.common_m1();
}
public void m2() {
// D3 specific implementation
}
}
Is there any reason why the second solution is better than the first?
Thank you,
Bobby