cet203 software development session 2a inheritance (programming in c#)

Post on 18-Jan-2018

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

CET203SOFTWARE DEVELOPMENT

Session 2AInheritance (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

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

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

}

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)

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?

Exercise 1

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

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

}

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

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

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 £ " + ___________ + ")");

Demonstration

EmployeeClass2A.sln

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

Exercise 3

EmployeeClass2B.sln

Exercise 3

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

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?

Additional code within HoursWorked

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

Exercise 4

EmployeeClass2C.sln

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

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!

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

Updated Class Diagram

PartTimeEmployee

- hoursWorked : int

+ CalcAnnualPay()

Employee

- name : String- department :

String# salary : int

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

protected access modifier

Exercise 5

EmployeeClass2D.sln

Exercise 5a

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

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

top related