15
Inheritance Inheritance Chapter: Chapter: 09 09 Lecture: 33 Lecture: 33 Date: 10.10.2012 Date: 10.10.2012

Lec 33 - inheritance

Embed Size (px)

Citation preview

Page 1: Lec 33 -  inheritance

InheritanceInheritance

Chapter: Chapter: 0909

Lecture: 33Lecture: 33

Date: 10.10.2012Date: 10.10.2012

Page 2: Lec 33 -  inheritance

Class HierarchiesClass Hierarchies

Page 3: Lec 33 -  inheritance

Public and Private Public and Private InheritanceInheritance

class employeeclass employee //base class//base class

{{ };};

class teacher : class teacher : publicpublic employee employee //publicly-//publicly-derivedderived

{{ };};

class teacher : class teacher : privateprivate employee employee//privately-derived//privately-derived

{{ };};

Page 4: Lec 33 -  inheritance

Public and Private Public and Private InheritanceInheritance

Page 5: Lec 33 -  inheritance

Levels of InheritanceLevels of Inheritance Classes can be derived from classes that Classes can be derived from classes that

are themselves derived.are themselves derived.

Page 6: Lec 33 -  inheritance

Levels of HierarchiesLevels of Hierarchies

Page 7: Lec 33 -  inheritance

Multiple InheritanceMultiple Inheritance

A class can be derived from more than one A class can be derived from more than one base classbase class

Page 8: Lec 33 -  inheritance

Multiple InheritanceMultiple Inheritance

Page 9: Lec 33 -  inheritance

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

Both inheritance and aggregation are class Both inheritance and aggregation are class relationships.relationships.

Inheritance is Inheritance is “kind of” “kind of” relationship - relationship - association.association.

Aggregation is Aggregation is “has a” “has a” relationship - associationrelationship - association Def. Def. AggregationAggregation - Several things grouped - Several things grouped

together together

or considered as a whole.or considered as a whole.

Page 10: Lec 33 -  inheritance

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

Both inheritance and aggregation are class Both inheritance and aggregation are class relationships.relationships.

Inheritance is Inheritance is “kind of” “kind of” relationship - relationship - association.association.

Aggregation is Aggregation is “has a” “has a” relationship - associationrelationship - association Def. Def. AggregationAggregation - Several things grouped - Several things grouped

together together

or considered as a whole.or considered as a whole.

Page 11: Lec 33 -  inheritance

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

Aggregation may occur when one object is an attribute Aggregation may occur when one object is an attribute of another. In other words, one class contains objects of another. In other words, one class contains objects of another class.of another class.

Example:Example:

class Aclass A

{{ };};

class Bclass B

{{

A objA; A objA; //object of class A is an attribute //object of class A is an attribute of class Bof class B

};};

Page 12: Lec 33 -  inheritance

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

Page 13: Lec 33 -  inheritance

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

Page 14: Lec 33 -  inheritance

Composition: Composition: A Stronger A Stronger AggregationAggregation

Composition is a stronger form of aggregation, Composition is a stronger form of aggregation, having all its plus two morehaving all its plus two more The part may belong to only one wholeThe part may belong to only one whole The lifetime of the part is the same as the lifetime of The lifetime of the part is the same as the lifetime of thethe

wholewhole While aggregation is a While aggregation is a “has a” “has a” relationship, relationship,

composition is a composition is a “consists of” “consists of” relationship. relationship.

Page 15: Lec 33 -  inheritance

Composition: Composition: A Stronger A Stronger AggregationAggregation

Composition is a stronger form of aggregation, Composition is a stronger form of aggregation, having all its plus two morehaving all its plus two more The part may belong to only one wholeThe part may belong to only one whole The lifetime of the part is the same as the lifetime of The lifetime of the part is the same as the lifetime of thethe

wholewhole While aggregation is a While aggregation is a “has a” “has a” relationship, relationship,

composition is a composition is a “consists of” “consists of” relationship. relationship.