cs313d: advanced programming language...2/14/2017 1 cs313d: advanced programming language computer...

14
2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 5: Inheritance & Polymorphism Computer Science department Lecture Contents Dr. Amal Khalifa,Spr17 What is Inheritance? Super-class & sub class Protected members Creating subclasses Polymorphism 2

Upload: others

Post on 17-Jul-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

1

CS313D: ADVANCED

PROGRAMMING LANGUAGE

Lecture 5: Inheritance & Polymorphism Computer Science

department

Lecture Contents

Dr. Amal Khalifa,Spr17

What is Inheritance?

Super-class & sub class

Protected members

Creating subclasses

Polymorphism

2

Page 2: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

2

OOP

Dr. Amal Khalifa,Spr17 3

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.

Dr. Amal Khalifa,Spr17

4

Page 3: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

3

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

Dr. Amal Khalifa,Spr17

5

Superclasses and Subclasses

Dr. Amal Khalifa,Spr17

Superclasse

s “more

general”

subclasses

“more

specific.”

every

subclass

object is an

object of its

superclass

6

Page 4: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

4

Protected Members

Dr. Amal Khalifa,Spr17

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

Example: CommissionEmployee Class

Inheritance hierarchy containing types of

employees in a company’s payroll application

Commission employees are paid a percentage of their

sales

Dr. Amal Khalifa,Spr17

8

Page 5: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

5

The code

A colon (:)

indicates

inheritance

Every C#

class directly

or indirectly

inherits

object’s

methods.

Dr. Amal Khalifa,Spr17

9

Dr. Amal Khalifa,Spr17 10

Page 6: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

6

Dr. Amal Khalifa,Spr17 11

virtual !!

A virtual

method is

ready to be

overridden in

the subclasses

Dr. Amal Khalifa,Spr17

12

Page 7: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

7

Override!!

Dr. Amal Khalifa,Spr17

To override a

base-class

method, a

derived class

must declare

a method with

keyword

override.

the same

signature

13

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

Dr. Amal Khalifa,Spr17

14

Page 8: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

8

Creating a new class

copy CommissionEmployee code, 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

Dr. Amal Khalifa,Spr17

15

The new class

Constructors

are not

inherited : The

derived-class

constructor,

before

performing its

own tasks,

invokes its

direct base

class’s

constructor

Dr. Amal Khalifa,Spr17

16

Page 9: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

9

Dr. Amal Khalifa,Spr17 17

Dr. Amal Khalifa,Spr17 18

Page 10: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

10

Tip !!

Dr. Amal Khalifa,Spr17

19

Test

Dr. Amal Khalifa,Spr17

20

Page 11: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

11

Dr. Amal Khalifa,Spr17 21

Polymorphism

Dr. Amal Khalifa,Spr17

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 12: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

12

Example

Same base

references ,

different

objects

Dr. Amal Khalifa,Spr17

23

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.

Dr. Amal Khalifa,Spr17

24

Page 13: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

13

Dr. Amal Khalifa,Spr17 25

Chapter 11

Chapter 12 : 12.3

Dr. Amal Khalifa,Spr17

26

Page 14: CS313D: ADVANCED PROGRAMMING LANGUAGE...2/14/2017 1 CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Lecture 5: Inheritance & Polymorphism department Lecture Contents Dr. Amal

2/14/2017

14

Case Study 27

Dr. Amal Khalifa,Spr17

28 Dr. Amal Khalifa,Spr17