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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Chapter 10: Introduction to Inheritance
Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS-2135 Object Oriented Programming
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Object Oriented Software Development
Inheritance using Java
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
CSC 142 Computer Science II Zhen Jiang West Chester University
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/14) Object Oriented Programming Joel Adams and Jeremy Frens Calvin.
Object Oriented Software Development
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming Abstractions Cynthia Lee CS106X. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism › Example: Expression.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Phil Tayco Slide version 1.0 Created Nov. 26, 2017
ATS Application Programming: Java Programming
Object Oriented Analysis and Design
Abstract Classes.
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Final and Abstract Classes
Extending Classes Through Inheritance
Chapter 7 Inheritance.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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

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 gaps

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 < 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

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