-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Run-time Polymorphism in C#
- Importance of base keyword in C#
Run-time Polymorphism in C#
- If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in C#. It is used to achieve runtime polymorphism. It enables user to provide specific implementation of the method which is already provided by its base class.
- When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Example:
Public class Animal{
//Overridden method
public void eat()
{
Console.WriteLine(“Animal is eating”);
}}
Public class Tiger : Animal{
//Overriding method
public void eat(){
Console.WriteLine(“Tiger is eating”);
}
static void Main() {
Tiger obj = new Tiger();
//This will call the child class version of eat()
obj.eat();
}
} //Output: Tiger is eating
Importance of base keyword in C#
- The base keyword in C# is a reference variable which is used to refer immediate parent class object.
- Whenever user create the instance of subclass, an instance of parent class is created implicitly which is referred by base reference variable.
- base keyword can be used at variable, method and constructor level.
Example:
public class Animal{
public string name=“Elephant”;
}
public class Dog : Animal{
public string name=“Bullet”;
public void printName(){
Console.WriteLine(Name);//prints Name of Dog class
Console.WriteLine(base.Name);//prints Name of Animal class
}
}
static void Main() {
Dog d=new Dog();
d.printName();
}
public class Animal
{
public Animal() {
Console.WriteLine("Animal Class Constructor");
}public string name ="Elephant";
//Overridden method
public void eat()
{
Console.WriteLine("Animal is eating");
}
}
public class Tiger : Animal
{
public Tiger() : base()
{
}
/*public Tiger()
{
Console.WriteLine("Tiger Class Constructor");
} */
public string name = "Royal Bengal Tiger";
public void printName()
{
//Console.WriteLine(name);//prints Name of Tiger class
Console.WriteLine(base.name);//prints Name of Animal class
}
//Overriding method
public void eat()
{
Console.WriteLine("Tiger is eating");
}
public void eatParentClass()
{
base.eat();
}
}
Animal animal = new Animal(); //Base class constructor will be called
// animal.eat();
Tiger obj = new Tiger(); //Both Base class and subclass constructor will be called
//This will call the child class version of eat()
obj.eat();
obj.eatParentClass();
obj.printName();