Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”"— Presentation transcript:

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

2 Common properties in classes

3 Inheritance Software 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.

4 Using Inheritance

5 Class Definition

6

7 Using Inheritance Only 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.

8 Implementation of inheritance

9 class Employee extends Person {... } class Manager extends Employee {... } class SalesPerson extends Employee {... } class Secretary extends Employee {... }

10 State of objects

11 Code Reuse By 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.

12 Changes in class hierarchy Changes 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.

13 Change in Property Definition for All Subclasses Suppose 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.

14 Change in Property Definition for All Subclasses Inheritance Is Not Available ◦ Redundancy arising ◦ Inconsistency ◦ software maintenance is difficult Inheritance Is Available ◦ Redundancy can be minimized ◦ Consistency ◦ software maintenance is simple

15 Change in Property Definition for Some Subclasses Let 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;

16 Change in Property Definition for Some Subclasses At 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.

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

18 Redefining getPay() method of Manager.

19 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.

20 Adding/Deleting a Class Thus, the deletion of subclasses that are not superclasses to other classes has a minimal impact on software maintenance.

21 Adding/Deleting a Class

22

23 Inheritance Chain Class 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.

24 Single inheritance A 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.

25 Single vs Multi in single Inheritance

26 Multiple Inheritance Multiple inheritance if a subclass in the hierarchy inherits properties from two or more superclasses in more than one inheritance path.

27 Multiple Inheritance

28 Problems Associated with Multiple Inheritance

29

30 Redefined attribute and method in multiple inheritance paths.

31 Ambiguities in language implementation

32 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.

33 Contract and Implementation Inheritance A 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.


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

Similar presentations


Ads by Google