Chapter 8 - Object Oriented Programming in C#
Chapter 8 - Object Oriented Programming in C#
C# 30
C# 3.0
Chapter 8 Object Oriented
Programming in C#
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Moving to OOP
In the next
ne t slides,
slides youll
o ll find an enhanced
p y class
version of the Employee
This version involves inheritance and polymorphism
to simulate a more complicated employees system
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Activating
A ti ti this
thi method
th d through
th
h a base
b
class
l
reference
f
variable, the base class version will be activated
Why?
?
What does this new keyword mean?
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
override vs.
vs new
A derived
deri ed class has two
t o override
o erride options:
options
It can override the method, using the override
keyword
This will generate the derived method execution when
activated through base reference variable
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
It p
prevents a common p
problem in C++
A programmer intending to override a method makes
a mistake in the signature,
g
, thus g
gets an overloaded
method instead of overridden method
In C# the compiler
can catch the signature
mistake
p
g
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Sealed Methods
It is possible to stop method o
overrideerride
y by
y defining
g an overrided method
ability
as a sealed one, using the sealed
keyword:
classDerived:Base
{
publicoverridesealed voidf1(){}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Sealed Classes
A more common usage of the sealed
keyword
y
is related to classes:
It is possible to prevent further derivation of a class,
by defining it as a sealed class
sealedclassPoint
{//}
When
Wh appropriate,
i
defining
d fi i a class
l
as
sealed is recommended, since it may
enable several optimizations
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
For example:
We have a reference to an Employee object and we
want to cast it to a Programmer reference
To be able to call a Programmer specific method: Program()
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# 30
The is Operator
Is a run-time
r n time type
t pe of an object compatible
type?
with a given
g
yp
The is operator is used in an expression of the form:
expression
is
i
i type
t
p
It will return true if expression
is not null and can be
cast to type
Employeee1=...;
Employeee1=
;
if(e1is Programmer)
{
Programmerp=(Programmer)e1;
p.Program();
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
The as Operator
To safely cast down we need to
Check if an object implements a specified interface
Cast it to a reference of the interface type
The as operator
p
p
performs both the check
and the cast operations
The as operator is used in an expression of the form:
expression as type
Employeee1=...;
Programmerp=e1as Programmer;
if(p!=null)
p.Program();
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
In C++
Calling a virtual method from base class constructor
results calling the base method version
During base class constructor activation the objects virtual
table pointer points to the base class virtual table
In C#
Calling a virtual method from base class constructor
results calling to the derived class
class method version
Note that the derived class constructor was not executed yet
At this point the method is running with fields default value(!)
C# 30
In C++
Derived class methods hide base class methods by
name
If a derived class defines a method with an identical name as
of its base, calling the method through a derived object would
always map to the derived method version
In C#
Derived class methods hide base class methods by
signature
If a derive class defines a method with an identical name as
of its base and the method is called through a derived object,
b th b
both
base and
dd
derived
i d method
th d versions
i
participate
ti i t as
candidates in the method call resolution
The best match between these two versions will be activated
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Chapter Summary
C# is a p
pure
re object oriented lang
language:
age
Everything is derived from object
There are many new keywords that support safe
inheritance
virtual
override
new
abstract
sealed
is &as
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel