28
CS 313 T: ADVANCED PROGRAMMING LANGUAGE Lecture 5: Inheritance & Polymorphism Computer Science department

CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

CS313T: ADVANCED

PROGRAMMING LANGUAGE

Lecture 5: Inheritance & PolymorphismComputer Science

department

Page 2: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Lecture Contents

What is Inheritance?

Super-class & sub class

Protected members

Creating subclasses

Polymorphism

2

Page 3: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

OOP

3

Page 4: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

OOP - Inheritance

A class can extend another class, inheriting all its data

members and methods while redefining some of them

and/or adding its own.

4

Page 5: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

OOP - Inheritance

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

5

Page 6: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Superclasses and Subclasses

Superclasse

s “more

general”

subclasses

“more

specific.”

every

subclass

object is an

object of its

superclass

6

Page 7: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Protected Members

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

7

Page 8: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Example: CommissionEmployee Class

Inheritance hierarchy containing types of

employees in a company’s payroll application

Commission employees are paid a percentage of their

sales

8

Page 9: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

The code

A colon (:)

indicates

inheritance

Every C#

class directly

or indirectly

inherits

object’s

methods.

9

Page 10: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

10

Page 11: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

11

Page 12: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

virtual !!

A virtual

method is

ready to be

overridden in

the subclasses

12

Page 13: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Override!!

To override a

base-class

method, a

derived class

must declare

a method with

keyword

override.

the same

signature

13

Page 14: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Creating a new class!!

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

14

Page 15: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Creating a new class

copy CommissionEmployeecode, pasted it into BasePlusCommissionEmployee

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

Copy & paste Inheritance

15

Page 16: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

The new class

Constructors

are not

inherited : The

derived-class

constructor,

before

performing its

own tasks,

invokes its

direct base

class’s

constructor

16

Page 17: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

17

Page 18: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

18

Page 19: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Tip !!19

Page 20: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Test20

Page 21: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

21

Page 22: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Polymorphism

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

22

Page 23: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Example

Same base

references ,

different

objects

23

Page 24: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Example

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.

24

Page 25: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

25

Page 26: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Chapter 11

Chapter 12 : 12.3

26

Page 27: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

Case Study27

Page 28: CS313T: ADVANCED PROGRAMMING LANGUAGE€¦ · Polymorphism a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass

28