cs313d: advanced programming language · protected members dr. amal khalifa,spr17 to enable a...

Post on 19-Jul-2018

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

Superclasses

“more

general”

subclasses

“more

specific.”

every subclass

object is an

object of its

superclass

6

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

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

Dr. Amal Khalifa,Spr17 11

virtual !!

A virtual

method is

ready to be

overridden in

the subclasses

Dr. Amal Khalifa,Spr17

12

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

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

Dr. Amal Khalifa,Spr17 17

Dr. Amal Khalifa,Spr17 18

Tip !!

Dr. Amal Khalifa,Spr17

19

Test

Dr. Amal Khalifa,Spr17

20

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

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

Dr. Amal Khalifa,Spr17 25

Chapter 11

Chapter 12 : 12.3

Dr. Amal Khalifa,Spr17

26

Case Study 27

Dr. Amal Khalifa,Spr17

28 Dr. Amal Khalifa,Spr17

top related