OO Relationships In Object Oriented world, if one class has to communicate with any other class (or classes), it has to communicate by using one of the Object Oriented Relationships. There are three (3) OO Relationships,
Inheritance or Generalization Aggregation o Weak o Strong (Also called as Composition or Containment) Association
Inheritance1 Inheritance is the process by which objects can acquire the properties of objects of other class. In OOP, inheritance provides reusability. For example, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes. The main advantage of inheritance is the ability to define new attributes and new methods for the subclass which are then applied to the inherited attributes and methods. [1 Quoted from: http://www.startvbdotnet.com/oop/default.aspx]
Horse/Zebra -- Key Example2
With inheritance, we define classes in terms of other classes. This can be a great shortcut if we have a family of classes with common aspects. Suppose you have a hierarchy of all the animals, except the zebra was omitted and you have been asked to add it in.
Wrong: define the zebra from scratch. Right: locate the Horse class. Introduce Zebra as a derived class of Horse.
Zebra inherits 90% of its behavior (no coding required).
In the Zebra class, see how Horse works, and then define (override) the few things that are features of Zebras but not Horses.
This is the key feature of inheritance -- arrange classes to factor out code duplication. [2 Quoted from: Stanford CS108, Winter 09]
Dept. of CS & IT, Superior University
1
Single Inheritance If a class has single (exactly one) base class, it’s called ‘Single Inheritance’. Example
Single Inheritance [Image Source: http://www.ajaxprojects.com/db/inheritance.jpg]
Single Inheritance
Dept. of CS & IT, Superior University
2
Multiple Inheritance If a class has multiple (more than one) immediate base classes (at same level), it’s called ‘Multiple Inheritance’. Example
(For Aggregation & Association see the respective handout).
Dept. of CS & IT, Superior University
3