inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to...
TRANSCRIPT
Inheritance“a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”
Common properties in classes
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.
Using Inheritance
Class Definition
Class Definition
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.
Implementation of inheritance
Implementation of inheritanceclass Employee extends Person { ... } class Manager extends Employee { ... } class SalesPerson extends Employee { ... } class Secretary extends Employee { ... }
State of objects
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.
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.
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.
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
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;
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.
Remove the getPay() method and define it individually in the subclass
Redefining getPay() method of Manager.
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.
Adding/Deleting a Class Thus, the deletion of subclasses
that are not superclasses to other classes has a minimal impact on software maintenance.
Adding/Deleting a Class
Adding/Deleting a Class
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.
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.
Single vs Multi in single Inheritance
Multiple InheritanceMultiple inheritance if a subclass
in the hierarchy inherits properties from two or more superclasses in more than one inheritance path.
Multiple Inheritance
Problems Associated with Multiple Inheritance
Problems Associated with Multiple Inheritance
Redefined attribute and method in multiple inheritance paths.
Ambiguities in language implementation
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.
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.