You are on page 1of 26

Session 5

Total.net / Programming in C# / Session 5 / 1 of 26

Session Objectives
Implement Polymorphism Implement Virtual Functions Discuss Abstract Base classes Discuss Interfaces Implement Interfaces

Implement Properties

Total.net / Programming in C# / Session 5 / 2 of 26

Polymorphism and virtual functions go hand in hand. Polymorphism allows you to implement methods of the derived class during run-time.

Polymorphism in C# (1)

Virtual functions come in handy when we need to call the derived class method from an object of the base class.
Total.net / Programming in C# / Session 5 / 3 of 26

Polymorphism in C# (2)

Total.net / Programming in C# / Session 5 / 4 of 26

Polymorphism in C# (3)

Total.net / Programming in C# / Session 5 / 5 of 26

Polymorphism in C# (4)

Total.net / Programming in C# / Session 5 / 6 of 26

Polymorphism in C# (5)

Total.net / Programming in C# / Session 5 / 7 of 26

Points to Remember
Polymorphism is intelligent overriding. Polymorphism decision as to which method to call is made at runtime. Polymorphism requires virtual functions, and virtual functions in turn require method overriding.

Total.net / Programming in C# / Session 5 / 8 of 26

Abstract Base Classes (1)


Abstract classes are classes that can be inherited from , but objects of that class cannot created

C# allows creation of Abstract Base classes by an addition of the abstract modifier to the class definition.

Total.net / Programming in C# / Session 5 / 9 of 26

Abstract Base Classes (2)

Total.net / Programming in C# / Session 5 / 10 of 26

Interfaces (1)
An interface is a pure abstract base class. It can contain only abstract methods and no method implementation.

A class that implements a particular interface must implement the members listed by that interface.

Total.net / Programming in C# / Session 5 / 11 of 26

Interfaces (2)

Total.net / Programming in C# / Session 5 / 12 of 26

Interfaces (3)
If we merge the last two codes and compile them we will get the following output -

Take another example -

Total.net / Programming in C# / Session 5 / 13 of 26

Interfaces (4)
If we now need to inherit a class MyImages

Total.net / Programming in C# / Session 5 / 14 of 26

Interfaces (5)
The output of the example is -

Total.net / Programming in C# / Session 5 / 15 of 26

Multiple Interface (1)


C# allows multiple interface implementations.

Total.net / Programming in C# / Session 5 / 16 of 26

Multiple Interface (2)

Total.net / Programming in C# / Session 5 / 17 of 26

Explicit Interface
Explicit interface implementation can be used when a method with same name is available in 2 interfaces.

Total.net / Programming in C# / Session 5 / 18 of 26

Interface Inheritance
New Interfaces can be created by combining together other interfaces.

Total.net / Programming in C# / Session 5 / 19 of 26

Properties (1)
Provide the facility to protect a field in a class by reading and writing to it through a feature called properties

Total.net / Programming in C# / Session 5 / 20 of 26

Properties (2)

Each time we need to access the field we need to call the set and get methods.

Total.net / Programming in C# / Session 5 / 21 of 26

Properties (3)

continued
Total.net / Programming in C# / Session 5 / 22 of 26

Properties (4)
continued

Total.net / Programming in C# / Session 5 / 23 of 26

Properties (5)

Total.net / Programming in C# / Session 5 / 24 of 26

Types of Properties
Read / Write Property
Read - Only Property

Write - Only Property


Total.net / Programming in C# / Session 5 / 25 of 26

More on Properties
Properties are logical fields Properties are an extension of fields. Unlike fields, properties do not correspond directly to a storage location. Unlike methods properties do not make use of parentheses

Total.net / Programming in C# / Session 5 / 26 of 26

You might also like