cs313t: advanced programming language€¦ · polymorphism a superclass reference at a subclass...
TRANSCRIPT
CS313T: ADVANCED
PROGRAMMING LANGUAGE
Lecture 5: Inheritance & PolymorphismComputer Science
department
Lecture Contents
What is Inheritance?
Super-class & sub class
Protected members
Creating subclasses
Polymorphism
2
OOP
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.
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
5
Superclasses and Subclasses
Superclasse
s “more
general”
subclasses
“more
specific.”
every
subclass
object is an
object of its
superclass
6
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
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
The code
A colon (:)
indicates
inheritance
Every C#
class directly
or indirectly
inherits
object’s
methods.
9
10
11
virtual !!
A virtual
method is
ready to be
overridden in
the subclasses
12
Override!!
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 ))
14
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
The new class
Constructors
are not
inherited : The
derived-class
constructor,
before
performing its
own tasks,
invokes its
direct base
class’s
constructor
16
17
18
Tip !!19
Test20
21
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
Example
Same base
references ,
different
objects
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.
24
25
Chapter 11
Chapter 12 : 12.3
26
Case Study27
28