Interface Inheritance - Example
In general, an abstract
class is used over an interface if certain class methods require a default
implementation. This lends itself
to the fact that abstract classes are used most often to define a set of common
states and behaviors so that inheriting classes are a type of that abstract
class. For example, a Pitbull
class that implements an abstract Dog
class makes sense because a pitbull is a dog.
Interfaces, on the other hand, tend to describe the abilities of an
inheriting class, such as a Tshirt class that implements a Washeable
interface. An unrelated concrete
class, such as Bedsheet, can also
implement the Washeable interface
because it too can be washed, even though is not a type of clothing.
As an example, two
interfaces can be declared:
public interface Edible
{
float numCalories();
}
public interface Cooked
{
float getTemp();
}