How do I ensure my abstract class's function can only operate on extenders of the same type as the c
Posted
by incrediman
on Stack Overflow
See other posts from Stack Overflow
or by incrediman
Published on 2010-03-28T23:55:06Z
Indexed on
2010/03/29
0:03 UTC
Read the original article
Hit count: 177
java
|abstract-class
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);
© Stack Overflow or respective owner