CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism
Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses Polymorphism
OOP 3
OOP - Inheritance 4 A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own.
OOP - Inheritance 5 Inheritance represents the is a relationship between data types (e.g. student/person) Existing class is the superclass (more general) New class is the subclass (more specific) C# supports only single inheritance each class is derived from exactly one direct superclass. Possible to Create hierarchy of classes
Superclasses and Subclasses 6 Superclasses more general subclasses more specific. every subclass object is an object of its superclass
Protected Members 7 To enable a subclass to directly access superclass instance variables, we can declare those members as protected in the superclass. an intermediate level of access between public and private. can be accessed by members of that superclass, by members of its subclasses. All public and protected superclass members retain their original access modifier when they become members of the subclass. Not recommended to enforce information hiding
Example: CommissionEmployee Class 8 Inheritance hierarchy containing types of employees in a company s payroll application Commission employees are paid a percentage of their sales
The code 9 A colon (:) indicates inheritance Every C# class directly or indirectly inherits object s methods.
10
11
virtual!! 12 A virtual method is ready to be overridden in the subclasses
Override!! 13 To override a base-class method, a derived class must declare a method with keyword override. the same signature
Creating a new class!! 14 Base-salaried commission employees receive a base salary plus a percentage of their sales. Class BasePlusCommissionEmployee: Data: first name, last name, social security number, gross sales amount, commission rate and base salary. (( All but the base salary are in common with class CommissionEmployee)). services: a constructor, and methods earnings, tostring and get and set for each instance variable ((Most are in common with class CommissionEmployee ))
Creating a new class 15 Copy & paste Inheritance copy CommissionEmployee code, pasted it into BasePlusCommissionEm ployee modify the new class to include a base salary and methods that manipulate the base salary. error prone time consuming. Too many copies of the same code bad maintenance Extend an existing class and add only the needed data/functionality Reusability Don t reinvent the wheel!!
The new class 16 Constructors are not inherited : The derivedclass constructor, before performing its own tasks, invokes its direct base class s constructor
17
18
19 Tip!!
Test 20
21
Polymorphism 22 a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass. The type of the referenced object, not the type of the variable, determines which method is called. Invoking a method on a subclass object via a superclass reference invokes the subclass functionality
Example 23 Same base references, different objects
Example 24 When the compiler encounters a virtual method call made through a variable, the compiler checks the variable s class type to determines if the method can be called.
25
26 Chapter 11 Chapter 12 : 12.3
27 Case Study
28