Lec 33 - inheritance

Preview:

Citation preview

InheritanceInheritance

Chapter: Chapter: 0909

Lecture: 33Lecture: 33

Date: 10.10.2012Date: 10.10.2012

Class HierarchiesClass Hierarchies

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

{{ };};

Public and Private Public and Private InheritanceInheritance

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.

Levels of HierarchiesLevels of Hierarchies

Multiple InheritanceMultiple Inheritance

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

Multiple InheritanceMultiple 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.

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.

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

};};

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

Aggregation: Aggregation: Classes Within Classes Within ClassesClasses

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.

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.

Recommended