Oo-relationships-inheritance (2)

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Oo-relationships-inheritance (2) as PDF for free.

More details

  • Words: 351
  • Pages: 3
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

Related Documents

Seniorstudio 2(2)(2)
June 2020 80
Seniorstudio 2(2)(2)
June 2020 86
Seniorstudio 2(2)(2)
June 2020 77
2-2
November 2019 81
2-2
May 2020 54
2(2)
April 2020 46