How do I ensure my abstract class's function can only operate on extenders of the same type as the c
- by incrediman
For example, let's say this is my abstract class:
abstract class A{
int x;
int y;
void foo(A fooMe);
}
...and B and C are two classes which extend A.
What I want is for B to only be able to call foo() on other Bs, and for C to only be able to call foo() on other Cs. But I want this to be out of the hands of the programmer who's extending my A class - that is, I want a way to ensure this functionality within As code alone.
What can I do? (If possible) I'd like to avoid any hack or generics solution that's too messy - I still want foo to be able to be called like this, for example:
B b=new B();
B bb=new B();
bb.foo(b);