I'm a relative newbie to thinking in OOP terms, and haven't yet found my ‘gut instinct’ as to the right way to do it. As an exercise I'm trying to figure out where you'd create the line between different types of objects, using the drinks on my desk as an example.
Assuming I create an object Drink
, that has attributes like volume
and temperature
, and methods like pour()
and drink()
, I'm struggling to see where specific drink 'types' come in.
Say I have a drink types of Tea
, Coffee
or Juice
, my first instinct is to sub-class Drink
as they have attributes and methods in common.
The problem then becomes both Tea
and Coffee
have attributes like sugars
and milk
, but Juice
doesn't, whereas all three have a variant
(Earl Grey, decaff and orange respectively).
Similarly, Tea
and Coffee
have an addSugar()
method, whereas that makes no sense for a Juice
object.
So does that mean the super-class should have those attributes and methods, even if all the sub-classes don't need them, or do I define them on the sub-classes, especially for attributes like variant
, where each sub-class has it's own list of valid values?
But then I end up with two addSugar()
methods, on the Tea
and Coffee
sub-classes.
Or given that I then end up putting all the attributes and methods on the super-class, as most are shared between at least a couple of the drink types, I wonder what was the point in sub-classing at all?
I fear I am just trying to abstract too much, but don't want to back myself in to a corner should I want to add a new type, such as Water
—with variant
still or sparkling—down the road.