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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

CET203SOFTWARE DEVELOPMENT

Session 2AInheritance (programming in C#)

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

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

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

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

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

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

}

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

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)

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

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?

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

Exercise 1

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

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

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

}

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

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

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

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

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

Exercise 2 – Fill in the gapsPartTimeEmployee ptEmp = new ___________;

ptEmp.Name = "Jo Smith";___________ = "Finance"; //set department___________ = 13000; //set salary___________ = 12; //set number hours worked

ptEmp.IncreaseSalary(10);

Console.WriteLine(ptEmp.Name + " from " + ptEmp.Department + " has annual pay £ " + ___________ + ")");

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

Demonstration

EmployeeClass2A.sln

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

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

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

Exercise 3

EmployeeClass2B.sln

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

Exercise 3

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

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

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?

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

Additional code within HoursWorked

public int HoursWorked { get { return hoursWorked; } set { if (value < 10 && Salary >= 10000) { Salary = 10000; } hoursWorked = value; } }

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

Exercise 4

EmployeeClass2C.sln

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

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());

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

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!

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

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

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

Updated Class Diagram

PartTimeEmployee

- hoursWorked : int

+ CalcAnnualPay()

Employee

- name : String- department :

String# salary : int

+ IncreaseSalary(percent : int)+ DecreaseSalary(percent : int)

protected access modifier

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

Exercise 5

EmployeeClass2D.sln

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

Exercise 5a

• What changes are necessary to the HoursWorked property in PartTimeEmployee to ensure that the correct salary restrictions are implemented?

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

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());