Inheritance and Polymorphism
ABC programming lesson
Inheritance and Polymorphism
Welcome to the module Inheritance and
Polymorphism.
Inheritance is a process of creating a new
class from an existing class.
Inheritance allows you to inherit attributes
and methods of the base class in the newly
created class.
Polymorphism is a feature of OOP that allows
the data members of a class to behave
differently based on their parameters and
data types.
In this module, you will learn about:
Implementing Inheritance
Method Overriding
Sealed Classes
Polymorphism
#1 - Implementing Inheritance
Explain the concept of inheritance
Describe how to implement inheritance in C#.
State and explain the protected access modifier.
State and explain the base keyword.
State and explain the new keyword.
Describe how to inherit constructors.
Inheritance
Inheritance specifies an “is a kind of" relationship
Inheritance is a class relationship
New classes specialize existing classes
Musician
Musician
Violin
Player
Violin
Player
Base class
Derived class
Generalization
Specialization
Is this a good
example of
inheritance ?
Purpose of Inheritance
Reusability
Reuse common methods and attributes
among classes without recreating them.
Use the same code in different
applications with little or no
changes.
Generalisation
Specialisation
Extension
Implementing Inheritance
Just need to insert a colon after the name of the derived class
followed by the name of the base class.
The derived class can now inherit all non-private methods and
attributes of the base class.
Example of Inheritance
Animal
Animal
Cat
Cat
"protected" Access Modifier
Protected variables or methods are accessed
only by:
The class in which they are declared
Derived class
Specified using the protected keyword.
"base" Keyword
Like “super” in Java
Access the variables and methods of the base class from the derived
class.
Cannot use the base keyword for invoking the static methods of the
base class.
Example of Using "base" Keyword
"new" Keyword
As a modifier to hide the methods or variables of the base class that
are inherited in the derived class.
Example of "new" Keyword
Constructor Inheritance
In C#, cannot inherit constructors like inherit methods.
The instance of the derived class will always first invoke the
constructor of the base class followed by the constructor of the
derived class.
Can explicitly invoke the base class constructor by using the base
keyword in the derived class constructor declaration
Constructor Chaining
Parent
Parent
F1
F1
F2
F2
Constructor Chaining
Rules for Constructors (MUST be clear and remember)
A default constructor will be automatically generated by the compiler
if no constructor is specified within the class.
The default constructor is ALWAYS a no-arg constructor.
If there is a constructor defined in the class, the default constructor is
no longer used.
If you don’t explicitly call a base class constructor in a derived class
constructor, the compiler attempts to silently insert a call to the base
class’s default constructor before executing the code in the derived
class constructor
Compiler-Generated Constructor Code
Can you see the Bugs?
How to fixed
Explicitly invoke the base class constructor
#2 - Method Overriding
Define method overriding.
Explain virtual and override keywords.
Method Overriding
Derived class to override or redefine the methods of the base class.
A method that is intended to be overridden is called a virtual method
The method implemented in the derived class from the base class is
known as the Overridden Base Method.
Implementing Method Overriding
Using appropriate "virtual" and "override" Keywords
To override a particular method of the base class in the derived class:
In the base class, declare the method using the “virtual” keyword.
In the derived class, declare the inherited virtual method using
the “override” keyword
Example of Implementing Method Overriding
Method Overriding Remarks
The overridden base method must have the same signature as the
virtual method
Cannot override a non-virtual or static method.
Both the override method and the virtual method must have the same
access level modifier.