![](https://www.opentextbooks.org.hk/sites/opentextbooks/files/book_profile/cc_icon/by-sa.png)
"Is-a" or "inheritance" (sometimes also called "generalization") relationships capture a hierarchal relationship between classes of objects. For instance, a "fruit" is a generalization of "apple", "orange", "mango" and many others. We say that fruit is an abstraction of apple, orange, etc. Conversely, we can say that since apples are fruit (i.e. an apple "is-a" fruit), that they inherit all the properties common to all fruit, such as being a fleshy container for the seed of a plant.
![media/image1.png](http://www.opentextbooks.org.hk/system/files/resource/8/8163/8177/media/image1.png)
In Java, inheritance relationships are declared using the extends keyword when declaring a class. A subclass "extends" a superclass, which means that the subclass is a concrete example of the more abstract superclass. For instance, the class Apple would extend the class Fruit.
public class Apple extends Fruit { ... }
In Java, a subclass is allowed to extend only a single superclass (no "multiple inheritance"). This restricts the interpretation of a hierarchal taxomony. A subclass is an embodiment of its superclass. It is useful to think of the subclass as not inheriting its superclass's behaviors but rather possessing these behaviors simply because it is the superclass (this is polymorphism). Extend really models what an object intrinsically is— its true "being" as it were. This is particularly useful when the superclass has particular concrete behaviors that all the subclasses should exhibit.
However, "is-a" can really be more of an "acts-like-a" relationship. This stems from the perspective that all objects are defined solely by their behaviors. We can never truly know what an object truly is, only how it acts. If two objects behave identically (at some abstract level) then we say that they are abstractly equivalent. What we need is a way to express the pure behavioral aspects of an object. Java has a the keyword implements which is used to show generalization of a pure behavioral abstraction called an interface. An interface has a similar syntax to a class, but only specifies behaviors in terms of the "signatures" (the input and output types) of the methods. For example we could have
public interface ISteerable { public abstract void turnLeft(); public abstract void turnRight(); } public class Truck implements ISteerable { public void turnLeft() { // turn the tires to the left } public void turnRight() { // turn the tires to the right } } public class Boat implements ISteerable { public void turnLeft() { // turn the rudder to the left } public void turnRight() { // turn the rudder to the right } }
Above, Trucks and Boats are not taxonomically related, but yet they both embody the behaviors of steer-ability, such as the ability to turn left or right. A person can pilot either a boat or a truck based solely on the fact that they both support turning left or right and not based on what sort of entity they are fundamentally. Note that as per the above definition, a class can implement multiple interfaces at the same time.
![media/image2.png](http://www.opentextbooks.org.hk/system/files/resource/8/8163/8177/media/image2.png)
- 1618 reads