Professional Documents
Culture Documents
Session Objectives
Implement Polymorphism Implement Virtual Functions Discuss Abstract Base classes Discuss Interfaces Implement Interfaces
Implement Properties
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)
Polymorphism in C# (3)
Polymorphism in C# (4)
Polymorphism in C# (5)
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.
C# allows creation of Abstract Base classes by an addition of the abstract modifier to the class definition.
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.
Interfaces (2)
Interfaces (3)
If we merge the last two codes and compile them we will get the following output -
Interfaces (4)
If we now need to inherit a class MyImages
Interfaces (5)
The output of the example is -
Explicit Interface
Explicit interface implementation can be used when a method with same name is available in 2 interfaces.
Interface Inheritance
New Interfaces can be created by combining together other interfaces.
Properties (1)
Provide the facility to protect a field in a class by reading and writing to it through a feature called properties
Properties (2)
Each time we need to access the field we need to call the set and get methods.
Properties (3)
continued
Total.net / Programming in C# / Session 5 / 22 of 26
Properties (4)
continued
Properties (5)
Types of Properties
Read / Write Property
Read - Only Property
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