2.1 Inheritance2 Java with Caffeine2 Java with Caffeine2.2 Example

2.1 Inheritance

Classes represent collections of uniform objects, i.e., objects with the same structure (fields and methods). However, in reality, things that belong to the same category are rarely that uniform but usually come in different variants; often these variants can be hierarchically classified.

Take as an example the kingdom of animals sketched in the following figure:


The Kingdom of Animals

An animal may be for instance a fish, a reptile, or a mammal. A mammal again may be a predator, a rodent, or a hoofed animal. A predator may be a wolf, a bear, or a cat, while a rodent may be a mouse or a rat, and so on.

A wolf and a bear are both predators, and a wolf and a mouse are both mammals. When calling an animal a "mammal", we highlight its property of breastfeeding its offspring and forget the more distinguishing properties of being a predator or being a rodent. Also in software we would like to group objects to a common class, if they have some common characteristics, even if they differ in some other features.

Similar to above animal classification, Java offers the possibility of categorizing classes in a hierarchical tree structure. If two classes ("predator" and "rodent") have a common ancestor ("mammal") in the tree, they share the common properties of this class but differ in other properties. If needed, objects of the different classes may be considered as objects of the ancestor class and treated alike.

© Wolfgang Schreiner; February 3, 2005

2.1 Inheritance2 Java with Caffeine2 Java with Caffeine2.2 Example