1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance...

Preview:

Citation preview

1

What is Inheritance?

The mechanism of deriving a new class from an old one is called inheritance

Classes organised into a ‘classification hierarchy’

Classes can inherit attributes and methods from other classes

Inheriting class can add extra attributes and or methods of its own

2

What is the purpose of Inheritance?

SpecialisationExtending the functionality of an existing

classGeneralisation

sharing commonality between two or more classes

Improved efficiency and greater robustness

3

Terminology

Derived class or subclass or child class. A class which inherits some of its attributes and

methods from another class Base class or superclass or parent class.

A class from which another class inherits ancestor.

A class’s ancestors are those from which its own superclasses inherit

descendant.A class’s descendants are those which inherit

from its subclasses

4

Terminology - a classification Hierarchy

Building

Commercial Public Domestic

Officeblock

Factory

Cathedral Hospital

Officeblock

ApartmentBlock

5

Terminology - a classification Hierarchy

Generalisation

Specialisation

Building

Commercial Public Domestic

Officeblock

Factory

Cathedral Hospital

Officeblock

ApartmentBlock

6

Terminology - a classification Hierarchy

Generalised

‘base class’Building

Commercial Public Domestic

Officeblock

Factory

Cathedral Hospital

Officeblock

ApartmentBlock

7

Terminology - a classification Hierarchy

Building

Commercial Public Domestic

Officeblock

Factory

Cathedral Hospital

Officeblock

ApartmentBlock

A ‘kind of’ Building

(AKO)

8

Terminology - a classification Hierarchy

Building

Commercial Public Domestic

Officeblock

Factory

Cathedral Hospital

Officeblock

ApartmentBlock

A ‘kind of’

Commercial building

(AKO)

9

Terminology - a classification Hierarchy

Building

Commercial Public Domestic

Officeblock

Factory

Cathedral Hospital

Officeblock

ApartmentBlock

Arrow in diagram

means

’inherits from’

10

Designing your classification hierarchy:‘A kind of’ or ‘a part of’?

Car

Vehicle

A car is ‘a kind of’ vehicle

car class can inherit from

vehicle class

11

Car

Car

Wheel

Vehicle

A car is ‘a kind of’ vehicle

car class can inherit from

vehicle class

A wheel isn’t ‘a kind of’ car.

A wheel is ‘a part of’ a car

- this is dealt with by aggregation

Designing your classification hierarchy:‘A kind of’ or ‘a part of’?

12

Need to analyse whether differences between objects are dependant on type (such as a house being different to a factory) or state. (different values of the class’s attributes)

Building

Short Building

TallBuilding

short building and tall buildingmight vary only in the value of

the height attribute - don’t need separate classes

Designing your classification hierarchy:Different classes or different states?

13

What do objects inherit?

Line

Attributes:start positionend position

Methods:draw

Coloured Line

Attributes:colour

Methods:set colour

A ‘coloured line’ is a kind of linethe coloured line class inherits all

the attributes and methods ofthe line class and adds attributes

and methods of its own

An object of the ‘coloured line’class has all the attributes and

methods of the ‘line’ base class aswell as the attributes and methods

added by the derived class

14

Specialisation

Extending the functionality of an existing class

eg a coloured line is a specialised kind of line

15

Specialisation

A class is both closed in that it has an

encapsulated, private part which cannot be affected by external manipulation

and open in that it allows itself to be used as part of a larger software unit.

16

Generalisation

Sharing commonality between two or more classes

If we were modelling animals in a zoo would we create a separate class for each animal type?

This would duplicate attributes such as legs and methods such as getAge()

Cow Whale EagleElephant

17

Generalisation

Helpful to place common elements (attributes and methods) in a shared base class and organise problem into an inheritance hierarchy.

Cow Whale EagleElephant

Animal

Mammal Bird

18

Generalisation

Sometimes this leads to the creation of abstract classes which can’t be instantiated directly

Cow Whale EagleElephant

Animal

Mammal Bird

Abstract classes

19

Generalisation

concrete classes can be instantiated directly

Cow Whale EagleElephant

Animal

Mammal BirdConcrete classes

20

C++ Syntax

The colon (:) operator to denote inheritance

Public and private derivation of object methods from a base class

The ‘protected’ keyword to allow derived classes to access inherited attributes

21

C++ Syntax

class BaseClass{private:

int x;

public: void setX(int x_in){x=x_in;} int getX(){cout<<x;}

}

A simple base classwith one private attribute x

and two public methods

22

Derived Class

class DerivedClass:public BaseClass{private:

int y;

public: void setY(int x_in){x=x_in;} int getY(){cout<<y;}

}

A simple derived classwith one private attribute y

and two public methods

23

Implementing Derived Class Object

main(){ DerivedClass obj1; obj1.SetY(10); obj1.SetX(5); obj1.getX(); obj1.getY();}

Obj1 is Derived class object, Which can

access the BaseClass Public Methods(SetX,

getX)

24

when the Derived Class Object is Created

Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the inherited data members from the base class and defined data members in the derived class itself.)

The base class constructor is called to initialize the data members inherited from the base class.

The derived class constructor is then called to initialize the data members added in the derived class

The derived-class object is then usable

25

When the Derived Class Object is Destroyed

When the object is destroyed (goes out of scope or is deleted) the derived class destructor is called on the object first

Then the base class destructor is called on the object

Finally the allocated space for the full object is reclaimed

26

Types of Derivation

The class can be derived in three visibility modes

publicprivateprotected

27

Public Derivation

When a class is derived in the public mode it does not change the mode of the inherited members in the derived class. The public members remain public and protected members remain protected for the derived class in public inheritance.

28

Private Derivation

When a class is derived in the private mode it changes the mode of the inherited members in the derived class. The public and protected members become the private members for the derived class. So the inherited members can be accessed only through the member functions of the derived class.

29

Protected Derivation

When a class is derived in protected mode it changes the mode of the inherited members in the derived class. The public and protected members become the protected members for the derived class in protected inheritance. So the inherited members can be accessed only through the member functions of the derived class.

30

C++ Syntax: public derivation

class DerivedClass: public BaseClass{private:

int y;

public:void setY(int y_in){y=y_in;}int getY(){cout<<y;}

}

A derived class.The colon operator means

the derived classinherits from the base class

31

C++ Syntax: public derivation

class DerivedClass: public BaseClass{private:

int y;public:

void setY(int y_in){y=y_in;} int getY(){cout<<y;}

}

the public derivation means that objects of the derived classcan access the public methodsand attributes of the base class

This is the most usual typeof derivation

32

C++ Syntax: public derivation

class BaseClass{private:

int x;public:

void setX(int x_in) {x=x_in;}int getX() {cout<<x;}

}

class DerivedClass: public BaseClass{private:

int y;public:

void setY(int y_in){y=y_in;}int getY(){cout<<y;}

}

main(){BaseClass base_object;DerivedClass derived_object;

base_object.setX(7);

derived_object.setX(12);derived_object.setY(1);base_object.getX();derived_object.getY();derived_object.getX();return 0;}

Object of the derivedclass can access methods

of the derived classand also methods of the

base class

33

C++ Syntax: private derivation

class DerivedClass: private BaseClass

{private:

int y;public:

void setY(int y_in); int getY();

}

Another derived class - the privatederivation means that objects

of the derived class can’taccess the public methods and

attributes of the base class - butthe methods of the derived class

can!This is the least common type

34

C++ Syntax: the ‘protected’ keyword

An object of a publicly derived class can access the public methods of the base class, but not the private attributes

Changing the private keyword in the base class to protected makes the attributes available to derived classes

35

Access Specifiers

Access Specifier

Accessible from own class

Accessible from derived class

Accessible from objects from outside the class

public Yes Yes Yes

protected Yes Yes No

private Yes No No

36

Effect of inheritance on the visibility of member

37

C++ Syntax: inheriting destructors

Constructors are not inherited. They are called implicitly or explicitly by the child constructor.

The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type).

But if we want a constructor that will accept an int, we have to define it explicitly

CM505.39 38

Advantages of Inheritance

• When one class is inherited from another class the code that provides a behavior required in the derived class need not have to be rewritten that is code be reused which increases reliability.

CM505.39 39

Advantages of Inheritance

• Code sharing can occur at several levels.

• For example, • At a higher level , many users can use the same class.

• At lower level, code can be shared by two or more classes.

CM505.39 40

Advantages of Inheritance

• When multiple classes inherit from the same base class, it guarantees that the behavior they inherit will be the same in all classes.

CM505.39 41

Advantages of Inheritance

• Inheritance permits the construction of reusable software components.

• Using inheritance one can concentrate on understanding the portion of the new system.

CM505.39 42

Advantages of Inheritance

• The development time in developing a system will be reduced rapidly.

Recommended