Consider the following classes:
public class X {};
public class Y : X {};
public class Z : X {};
public class A {
public bool foo (X bar) {
return false;
}
};
public class B : A {
public bool foo (Y bar) {
return true;
}
};
public class C : A {
public bool foo (Z bar) {
return true;
}
};
Is there a way to achieve the next desired exit?
A obj1 = new B();
A obj2 = new C();
obj1.foo(new Y()); // This should run class B implementation and return true
obj1.foo(new Z()); // This should default to class A implementation and return false
obj2.foo(new Y()); // This should default to class A implementation and return false
obj2.foo(new Z()); // This should run class C implementation and return true
The problem I am facing is that an implementation of class A is always called regardless of the arguments passed.
source
share