Presentation is loading. Please wait.

Presentation is loading. Please wait.

CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)

Similar presentations


Presentation on theme: "CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)"— Presentation transcript:

1 CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)

2 Objectives Understand the terminology used in discussing inheritance Implement inheritance in C# applications Create objects from classes/subclasses and access super/sub class properties and methods Customise property behaviours Know when and how to use the protected access modifier

3 Terminology Superclass can be called a base class A class which inherits from it is called a derived class or subclass or extended class Can also use the terms parent class and child class A derived class can be further extended to form a whole hierarchy The full list of parent classes from which a child class is derived is known as the ancestors of the class Inheritance is transitive i.e. a base class inherits all of the attributes and methods of its parent

4 C# implementation No special features are required to create a superclass. Subclass specifies it is inheriting features from a superclass using a colon : class MySubclass : MySuperclass { // additional instance variables and // additional methods }

5 The Employee class In earlier sessions we created a class to model the characteristics of an employee Employee - name : String - department : String - salary : int + Employee(name : String, dept : String, salary : int) + IncreaseSalary(percent : int) + DecreaseSalary(percent : int)

6 Different types of Employee Let us now extend this model to include special characteristics of part-time employees: – They work for only a specified number of hours in the week – Their annual pay is a calculated from their salary – based on the number of hours worked How can we represent this on a class diagram?

7 Exercise 1 How can we extend the class diagram for Employee to represent the additional characteristics of part-time employees?

8 C# implementation No changes are necessary to the superclass (Employee). The PartTimeEmployee subclass specifies it is inheriting features from Employee using a colon: class PartTimeEmployee: Employee { // additional instance variables and // additional methods }

9 The full class class PartTimeEmployee : Employee { private int hoursWorked; public int HoursWorked { get { return hoursWorked; } set { hoursWorked = value; } public PartTimeEmployee() { hoursWorked = 0; } public double CalcAnnualPay() { return (Salary * hoursWorked / 37); } Note use of property inherited from Employee

10 Creating objects We can create objects of both the super and subclasses using the standard notation The subclass inherits all of the general features of the subclass so we can access its properties/methods directly In addition we can access the specialized properties/methods defined by the subclass

11 Exercise 2 – Fill in the gaps

12 Demonstration EmployeeClass2A.sln

13 Extending the model further We are now going to implement some restrictions on employee salaries The company decides that the minimum salary for employees is £15000 We can enforce this restriction by modifying our set method for the Salary property in the Employee class We will add the condition that if we try to set a salary value of < 15000 it is adjusted to 15000

14 Exercise 3 EmployeeClass2B.sln

15 Exercise 3 Write the modified code for the Employee class Salary property to enforce the restriction that the minimum salary is 15000.

16 More salary restrictions! Let us assume that our system is now in use within the company Whenever a salary is set using the Salary property, a minimum value of 15000 is assigned The company now decides to change the restrictions for part- time employees! If a part-time employee works less than 10 hours a week then their salary is capped at 10000 We can add the code to implement this to the set method for the HoursWorked property Or can we?

17 Additional code within HoursWorked public int HoursWorked { get { return hoursWorked; } set { if (value = 10000) { Salary = 10000; } hoursWorked = value; }

18 Exercise 4 EmployeeClass2C.sln

19 Exercise 4 What will be the result of the following statements (assume ptEmp is a PartTimeEmployee): 1.ptEmp.Salary = 13000; ptEmp.HoursWorked = 12; Console.WriteLine(“Salary is “ + ptEmp.Salary()); 2.ptEmp.Salary = 13000; ptEmp.HoursWorked = 8; Console.WriteLine(“Salary is “ + ptEmp.Salary());

20 A problem? Cannot use the Salary property as it sets a minimum limit of the salary to £15k Instead we need to access the salary instance variable directly This will not work because the salary instance variable is in the superclass and is private A private instance variable cannot be used outside the class, even by subclasses If this were the case then the principle of information hiding would be destroyed!

21 Solution One solution would be to make the salary instance variable public This not be recommended as the system would no longer conform to the principle of information hiding The protected (# on a class diagram) access modifier is provided for this purpose A protected instance variable or method can used within its own class or in any subclasses derived from that class but it cannot be used by outside classes

22 Updated Class Diagram PartTimeEmployee - hoursWorked : int + CalcAnnualPay() Employee - name : String -department : String # salary : int + IncreaseSalary(percent : int) + DecreaseSalary(percent : int) protected access modifier

23 Exercise 5 EmployeeClass2D.sln

24 Exercise 5a What changes are necessary to the HoursWorked property in PartTimeEmployee to ensure that the correct salary restrictions are implemented?

25 Exercise 5b Using the new version of the classes what will be the result of the following statements (assume ptEmp is a PartTimeEmployee): 1.ptEmp.Salary(13000); ptEmp.HoursWorked = 12; Console.WriteLine(“Salary is “ + ptEmp.Salary()); 2.ptEmp.Salary(13000); ptEmp.HoursWorked = 8; Console.WriteLine(“Salary is “ + ptEmp.Salary());


Download ppt "CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)"

Similar presentations


Ads by Google