inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to...

33
Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Upload: eileen-shields

Post on 18-Dec-2015

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Inheritance“a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Page 2: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Common properties in classes

Page 3: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

InheritanceSoftware Reuse: Generalized

properties defined in superclasses should be made available to subclasses without having to declare them explicitly in the subclasses.

In object-oriented programming, such reuse is possible via inheritance.

Inheritance is the ability of a subclass to take on the general properties of super-classes in the inheritance chain.

Page 4: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Using Inheritance

Page 5: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Class Definition

Page 6: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Class Definition

Page 7: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Using InheritanceOnly downward propagation of

properties from superclasses to subclasses is permissible.

There is no upward propagation of properties in object-oriented programming.

The Superclass–Subclass relationship in a class hierarchy is denoted in the code by the keyword extends.

Page 8: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Implementation of inheritance

Page 9: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Implementation of inheritanceclass Employee extends Person { ... } class Manager extends Employee { ... } class SalesPerson extends Employee { ... } class Secretary extends Employee { ... }

Page 10: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

State of objects

Page 11: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Code ReuseBy allowing information of a

superclass to be taken on by subclasses, the information is said to be reused at the subclass level.

All newly created instances of the subclasses would have as part of their definition the inherited information.

Page 12: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Changes in class hierarchyChanges to software specification

are inevitable.Let us consider how changes in a

class hierarchy impact software maintenance as a whole. ◦Change in property definition for all

subclasses. ◦Change in property definition for

some subclasses. ◦Adding/deleting a class.

Page 13: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Change in Property Definition for All SubclassesSuppose a change in

representational scheme of the employee number

This change will affect not only the attribute employee number but also the method getEmployeeNumber() and possibly other classes that inherit employee number.

Page 14: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Change in Property Definition for All SubclassesInheritance Is Not Available

◦Redundancy arising◦Inconsistency◦software maintenance is difficult

Inheritance Is Available ◦Redundancy can be minimized◦Consistency◦software maintenance is simple

Page 15: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Change in Property Definition for Some SubclassesLet us assume the following for a

HomeCare employee: ◦a manager—basic salary plus

allowance; ◦a salesperson—basic salary plus

commission; ◦a secretary—basic salary; ◦a technician—basic salary; ◦a clerk—basic salary;

Page 16: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Change in Property Definition for Some SubclassesAt the Employee class, a getPay()

method is defined to return the monthly pay of an employee since the method applies to all classes of employee.

Page 17: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Remove the getPay() method and define it individually in the subclass

Page 18: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Redefining getPay() method of Manager.

Page 19: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Adding/Deleting a Class Adding a class into an existing class

hierarchy can be detrimental to the stability of the hierarchy.

It is always recommended that the addition of a new class be created as a subclass in the class hierarchy.

Subclassing is specialization and is thus a desired design practice in object-oriented software engineering because it has minimal impact on software maintenance.

Page 20: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Adding/Deleting a Class Thus, the deletion of subclasses

that are not superclasses to other classes has a minimal impact on software maintenance.

Page 21: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Adding/Deleting a Class

Page 22: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Adding/Deleting a Class

Page 23: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Inheritance ChainClass hierarchies whose classes

have only one parent or superclass.

Such hierarchies are said to exhibit single inheritance. Besides single inheritance, there is also multiple inheritance.

The path of inheritance over the classes is known as the inheritance chain.

Page 24: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Single inheritanceA single inheritance chain can be

single- or multilevel.In a single-level single

inheritance chain, there is only one level of superclass that a subclass can inherit properties from.

In contrast, in a multilevel single inheritance chain, a subclass can inherit from more than one level of superclasses.

Page 25: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Single vs Multi in single Inheritance

Page 26: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Multiple InheritanceMultiple inheritance if a subclass

in the hierarchy inherits properties from two or more superclasses in more than one inheritance path.

Page 27: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Multiple Inheritance

Page 28: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Problems Associated with Multiple Inheritance

Page 29: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Problems Associated with Multiple Inheritance

Page 30: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Redefined attribute and method in multiple inheritance paths.

Page 31: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Ambiguities in language implementation

Page 32: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Contract and Implementation Parts Basically, a method has two parts:

contract and implementation. The contract part is also known as

the method signature and specifies the name of the method, its return type and formal parameters (if any).

The implementation part contains statements that are executed when a method is called.

Page 33: Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Contract and Implementation InheritanceA class inherits property

definitions and the way the properties are implemented from its superclasses in a singleinheritance chain.

In other words, a class in a single inheritance chain inherits both the contract and implementation parts of the superclasses’ properties.