"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... " new
Trang 1Inheritance and Polymorphism
ABC programming lesson
Trang 2Inheritance and Polymorphism
Welcome to the module Inheritance and
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
Trang 3#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.
Trang 4 Inheritance specifies an “is a kind of" relationship
Inheritance is a class relationship
New classes specialize existing classes
Musician
Violin Player
Violin Player
Trang 5Purpose 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
Trang 7Example of Inheritance
Animal
Cat
Trang 8"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.
Trang 9" 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.
Trang 10Example of Using " base " Keyword
Trang 11" new " Keyword
As a modifier to hide the methods or variables of the base class that are inherited in the derived class.
Trang 12Example of " new " Keyword
Trang 13Constructor 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
Trang 14Constructor Chaining
Parent
F1
F2
Trang 15Constructor Chaining
Trang 16Rules 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
Trang 17Compiler-Generated Constructor Code
Trang 18Can you see the Bugs?
Trang 19How to fixed
Trang 20Explicitly invoke the base class constructor
Trang 21#2 - Method Overriding
Define method overriding.
Explain virtual and override keywords.
Trang 22Method 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.
Trang 23Implementing 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
Trang 24Example of Implementing Method Overriding
Trang 25Method 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.
Trang 26Declaring new Methods
Trang 27Calling the Base Class Method
Trang 28#3 - Sealed Classes
Define sealed classes in C#
Describe the purpose of sealed classes.
Trang 29Sealed Classes
Class that prevents inheritance.
Like “final” class in Java
To declare, use sealed keyword before class keyword.
If a class tries to derive a sealed class, the C# compiler generates an error.
Trang 30Purpose of Sealed Classes
Ensure Security
Protect from Copyright Problems
Prevent Inheritance
Trang 31Using Sealed Class Guidelines
If overriding the methods of a class might result in in unexpected functioning of the class
To prevent any third party from modifying your class
Trang 32#4 - Polymorphism
Define polymorphism in C#
Explain how to implement polymorphism
Describe compile and run-time polymorphism
Trang 33Define polymorphism in C#
Polymorphism means existing in multiple forms
The ability of an entity to behave differently in different situations
Polymorphism allows methods to function differently based on the parameters and their data types
Implement polymorphism in C# through:
Method overloading (Compile-time Polymorphism)
Method overriding (Run-time Polymorphism)
Trang 34Example of polymorphism in C#
A B C
Trang 35Example of polymorphism in C#
Trang 36Compile-time and Run-time Polymorphism
Compile-time Polymorphism
Implemented through method overloading
Executed at the compile-time
Referred to as static polymorphism
Trang 37That’s about all for today!