Sei sulla pagina 1di 12
UNIT2 [31 Object-oriented festuresin asses 2.3 Extending Classes for Reusability — Inheritance Objectives 1, Define inheritance. 2. Use the “is-a’ relationship due co inheritance. 3. Use the ‘has-a’ relationship between classes. Introduction Inheritance is also called generalisation, because it captures a hierarchical relationship between classes of objects. For instance, a “fruit” is a generalisation ‘of “apple”, “orange”, “mango” and many others. Conversely, we can say that since apples ate fruits chey inherit all the properties common to al Fruits. ‘Therefore, one of the powerful advantages of inheritance is that modules with sufficiently similae interfaces can be commanded by shared program code, reducing the complexity of the program. Ic allows a sub-class to access the members of its parent class. Suppose the Sea¢ class defines a method called staf ttunber() and 4 property called caiculatesalary(). Each of its sub-classes (anager, Clerk, and Executive) will “inherit” these members, meaning that the programmer only needs to write the code for them once. Each sub-class can alter its inherited traits. When an object or class inherits its traits from more than one ancestor class, its called multiple inheritances. ‘An introductory material about inheritance can be found in the following reading. @ Reading Malik, D S, Java Programming: From Problem Analysis to Program Design, “Inheritance, p. 680-682. 32] WAWASAN OPEN UNIVERSITY 1102 Computing Example of inheritance in a payroll calculation system ‘Suppose that you need a payroll calculation system to calculate the monthly salary for each staff member in the company. By analysing the problem, you determine the following requirements: 1, The details of monthly salary for a staff member include name, staff number and salary. 2. The basic salary determines the salaries of most staff. The salaries of managerial grade staf are the sums of basic salaties and commissions. Based on the above requirements, you could design two classes, stafft and Manager as shown in Figure 2.17. staff Manager! name Sting = name String = number: String — number: String = basleSalary :double — basicSalary :double = commission : double “+ gettvamet) String Ison double ++ getNumber):String “getName )= String ++ getBasicSalary(): double {+ getNumber)=String 4+ setName theName : Sting) + getBasicSalary(}: double ++ setNumber(theNumber Sting) ++ setNlame(theName : String) “+ setBasicSalaryitheBasicSalary double) | | + sotNumber(theNumber : Sting) + setBasleSalaryitheBasicSalary double) ++ setCommissiontheCommission : double) Figure 2.17 The classes scat#i and wenagert Based on the class designs shown in Figure 2.17, definitions of class state and anager? can be written in che Java programming language. ‘Compating the designs of clases Sta£e1 and tsnager:, itis clear that large portions of the two class definitions are the same. We mentioned that itis always undesirable to have duplicated code in class definitions — if you need to modify the common code, each duplicated code needs modifying, and such operations are always error prone and tedious. To illustrate such a scenario, the designs of the class stat#2 and Manager? are usually drawn in a way to highlight the extension as shown in Figure 2.18 ‘The arrow in Figure 2.18 pointing from Manager? class to S:a#£2 class indicates, that vanager2 isan extension of the Staf £2 class. Icis known as inheritance, a way to form new classes (instances of which are called objects) using classes that have already been defined. The former, known as derived classes, take over (or inherit) attributes and behaviour of the latter, which are referred to as base classes. It is inended to help reuse of existing code with little or no modification, uNir2 object-oriented festuresin dass Staff ~ name + String ~ number : string = basicSalary : double + getName( } + String + getNumber( } : string + gotBasicsalary( ) : double + setiiame (theName : String) + setiumber (theumber : String) 4 setbasicSalary (theasicSalary + double) ee anager2 = conniseion + double + seecommission() : double + getConmission(theConmission : double} Figure 2.18 The designs for classes seat t2 and vanager2, For implementation purposes, Java enables you to reuse an existing class definition with the keyword extends. Figure 2.19 and Figure 2.20 illustrate this technique using clases staf f2 and Manager? respectively. // befinition of clase Staff? public class staff? { I) Attributes private String names //the staft name private String number; //ohe staf? number private double basicSalary; //The basic salary cee public String getWame() ( hod return name; public String getwumber() | geturn numbers public double getBasicsalary() [ return basieSalary: 11 Set Method public void setName (String theName) ( name ~ theNane;