Spaces:
Running
Running
Parent-Ancestor vs Child; super vs sub
Browse files
Week 5: Class hierarchies/01A. Class hierarchies
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Often, when programming an object, you come across a situation where 2 classes have COMMON FEATURES.
|
| 2 |
+
An example could be the classes Teacher and Student.
|
| 3 |
+
Both classes would have at least the attributes name and email.
|
| 4 |
+
|
| 5 |
+
However, in addition to the common attributes,
|
| 6 |
+
the classes would also have separate attributes:
|
| 7 |
+
for example, a teacher would not have an attribute for the number of credits, and
|
| 8 |
+
a student would not have an attribute for the course being taught.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
It is of course possible to create 2 separate classes with similar attributes.
|
| 12 |
+
However, it would often make more sense to use class hierarchies in such a situation.
|
| 13 |
+
|
| 14 |
+
The idea of a class hierarchy is that classes can have DESCENDANTS and corresponding PARENTS (or "ancestors").
|
| 15 |
+
A class inherits characteristics from its parent.
|
| 16 |
+
If a class has multiple children, they all share certain common characteristics.
|
| 17 |
+
In addition to these, the children may, of course, have their own characteristics that are different from one another.
|
| 18 |
+
|
| 19 |
+
In addition to the client-supplier relationship discussed earlier,
|
| 20 |
+
the inheritance relationship is another important relationship in object oriented programming
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
Parent and child classes
|
| 27 |
+
If class A inherits class B,
|
| 28 |
+
class A is said to be a child class of B.
|
| 29 |
+
Similarly, B is the parent class of A.
|
| 30 |
+
|
| 31 |
+
The following figure illustrates the inheritance relationships:
|
| 32 |
+

|
| 33 |
+
|
| 34 |
+
In the picture, for example
|
| 35 |
+
- A Vehicle is a superclass of the categories Car and Motorcycle
|
| 36 |
+
- The classes Hatchback and Truck are subclasses of the class Car
|
| 37 |
+
- All other classes inherit the properties of the 'Vehicle' classes:
|
| 38 |
+
for example, Car and Motorcycle are both vehicles, but they also have their own, different properties.
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
The inheritance relationship therefore means that the inheriting class is also a parent class.
|
| 42 |
+
For example, a hatchback is also a car and a vehicle.
|
| 43 |
+
|
| 44 |
+
Thus, the class Dog could naturally inherit the class Animal,
|
| 45 |
+
but the class Cat could not inherit the class Dog (because a dog is an animal, but a cat is not a dog).
|
| 46 |
+
|
| 47 |
+
Similarly, the class Paperback could inherit the class Book,
|
| 48 |
+
but the class Page could not (paperback is a book, page is not a book, even though a book has pages).
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|