Let's say I have one class Foo
that has a bunch of logic in it and another class Bar
which is essentially the same. However, as Foo
and Bar
are different (but related) entities I need the difference to be apparent from my code (i.e. I can tell whether an instance is a Foo
or a Bar
)
As I was whacking this together without much thought I ended up with the following:
public class Foo {
/* constructors, fields, method, logic and what-not */
}
public class Bar extends Foo {
/* nothing here but constructors */
}
Is this OK? Is it better to make Bar
a composite class? e.g:
public class Bar {
private Foo foo;
/* constructors and a bunch of wrapper methods that call
into foo */
}
Or even, while we're at it, something much more low-tech:
public class Foo {
/* constructors, fields, method, logic and what-not */
private boolean isABar; // Could be an enum
}
What do you think? How do you deal with these 'marker classes'?
As an example of how my code may wish to treat Foo
and Bar
differently, my code would need to be able to do stuff like List<Foo>
and List<Bar>
. A Foo
couldn't go in a List<Bar>
and vice versa.