You are on page 1of 28

Session 2

Total.net / Programming in C# / Session 2 / 1 of 28

Session Objectives
Discuss Inheritance Explain different types of Inheritance

Discuss Method Overriding Explain Polymorphism


Discuss and Appreciate Virtual Functions

Total.net / Programming in C# / Session 2 / 2 of 28

Introduction
Object Oriented Programming enables you to declare a new class as a descendant of an existing class.

The descendant class (also known as the child class) inherits the properties and the methods of its parent class (also known as a base class) The inherited class can define new properties and methods and if required, override the methods of the base class.
Total.net / Programming in C# / Session 2 / 3 of 28

Benefits of Inheritance

Total.net / Programming in C# / Session 2 / 4 of 28

Types of Inheritance

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

Types of Inheritance

Total.net / Programming in C# / Session 2 / 6 of 28

Single Inheritance
We define a class BaseIO with some properties and methods

Total.net / Programming in C# / Session 2 / 7 of 28

Single Inheritance
Now we define a new class ImageIO which inherits from class BaseIO

Total.net / Programming in C# / Session 2 / 8 of 28

Single Inheritance
The ImageIO class can use the Delete() method since it inherits from the BaseIO class.

Total.net / Programming in C# / Session 2 / 9 of 28

Access Modifiers
Access Modifiers are responsible for the visibility of the class members to an object and to the inherited class.

Total.net / Programming in C# / Session 2 / 10 of 28

Access Modifiers

Total.net / Programming in C# / Session 2 / 11 of 28

Access Modifiers
Consider the following example -

Total.net / Programming in C# / Session 2 / 12 of 28

Access Modifiers
If we use a Private or a Protected access modifier the following would be the results -

Total.net / Programming in C# / Session 2 / 13 of 28

Constructors
Constructors are a special type of member functions of a class - called automatically when an object is created. Constructors are used to initialize class instances. In the case of the base class, the runtime simply invokes the constructor of the class.

In case of an inherited class, the constructor of the base class is invoked first and then the constructor of the inherited class is invoked.
Total.net / Programming in C# / Session 2 / 14 of 28

Constructors

Total.net / Programming in C# / Session 2 / 15 of 28

Method Overriding
Inheritance provides the flexibility of overriding methods of the base class. The following figure illustrates this -

Total.net / Programming in C# / Session 2 / 16 of 28

Method Overriding

Total.net / Programming in C# / Session 2 / 17 of 28

Polymorphism
The word polymorphism means many forms. Languages that do not support Polymorphism are not considered as object oriented languages. They are known as object based languages. Ada is an object based language.
Total.net / Programming in C# / Session 2 / 18 of 28

Method Overloading
The process by which two methods can co-exist with the same name is known as Method Overloading.
Unique features of a method help you differentiate one method from another. These unique features include Total.net / Programming in C# / Session 2 / 19 of 28

Method Overloading

Total.net / Programming in C# / Session 2 / 20 of 28

Virtual Functions
Virtual functions enable the possibility for a function to be polymorphic when it is overridden in one or more inherited classes. To understand Virtual functions, consider the following example -

Total.net / Programming in C# / Session 2 / 21 of 28

Virtual Functions

Total.net / Programming in C# / Session 2 / 22 of 28

Virtual Functions
We will now call the functions -

We will get the output -

Total.net / Programming in C# / Session 2 / 23 of 28

Virtual Functions
Virtual functions are defined just as any normal function except that they need to be preceded with the keyword Virtual . Virtual returnDataType MethodName (Parameters) Let us re-write the previous example using Virtual function -

Total.net / Programming in C# / Session 2 / 24 of 28

Virtual Functions

Total.net / Programming in C# / Session 2 / 25 of 28

Virtual Functions
The output of the new code would be -

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

Virtual Functions
Virtual functions consume more resources than normal functions and are slower. It is not feasible to mark every method as virtual

Total.net / Programming in C# / Session 2 / 27 of 28

Guidelines
Check if the member function is called by other member functions. Check if the called member function is overridden in the inherited class.

Total.net / Programming in C# / Session 2 / 28 of 28

You might also like