java class A extends class B, and method override
class, extends, java, overriding
Solution
Since ClassY extends ClassX, then you can remove `private ClassY b` from class B. Then you can just set your instance of ClassX to the `a` instance variable. This allows `foo()` to be inherited in class B, but still use the same logic and instance variable.
public class A {
protected ClassX a
public void foo() {
// operations on a;
}
}
public class B extends A {
// do something to set an instance of ClassY to a; for example...
public void setClassY(ClassY b){
this.a = b;
}
}
Problem
``` public class A { protected ClassX a; public void foo() { operations on a; } } public class B extends A { private ClassY b; // ClassY extends ClassX @Override public void foo() { //wanna the exact same operation as A.foo(), but on b; } } ``` Sorry for such a not clear title. My question is: in class B, when I call foo(), and I want the exact same operation as class A have on a. How do I achive that and without duplicate the same code from A? If i leave out foo() in class B, would it work? Or whats happening when I call super.foo() in foo();