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 B
s, and for C
to only be able to call foo()
on other C
s. 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 A
s 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);
Edit: I'm now willing to accept a solution which uses generics, if anyone has any ideas... (I couldn't think of one)?