Professional Documents
Culture Documents
Session Objectives
Discuss Inheritance Explain different types of Inheritance
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
Types of Inheritance
Types of Inheritance
Single Inheritance
We define a class BaseIO with some properties and methods
Single Inheritance
Now we define a new class ImageIO which inherits from class BaseIO
Single Inheritance
The ImageIO class can use the Delete() method since it inherits from the BaseIO class.
Access Modifiers
Access Modifiers are responsible for the visibility of the class members to an object and to the inherited class.
Access Modifiers
Access Modifiers
Consider the following example -
Access Modifiers
If we use a Private or a Protected access modifier the following would be the results -
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
Method Overriding
Inheritance provides the flexibility of overriding methods of the base class. The following figure illustrates this -
Method Overriding
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
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 -
Virtual Functions
Virtual Functions
We will now call the functions -
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 -
Virtual Functions
Virtual Functions
The output of the new code would be -
Virtual Functions
Virtual functions consume more resources than normal functions and are slower. It is not feasible to mark every method as virtual
Guidelines
Check if the member function is called by other member functions. Check if the called member function is overridden in the inherited class.