Methods Overriding in C#
Whenever derived class characterizes the same strategy as characterized in its base class, it is known as method overriding It is utilized to accomplish runtime polymorphism. It empowers you to give explicit usage of the technique which is now given by its base class. Overriding methods override a base class technique in a derived class by making a technique with the same name and parameters by utilizing virtual and override keywords.
To perform this task you have to utilize virtual keyword with base class technique and abrogate catchphrase with override keyword with derived class strategy.
So Let's see a simple program which demonstrates method overriding as below.
Programa
using System;
namespace Method_Overriding
{
public class A /* Base Class*/
{
public virtual void Get_Detail()
{
Console.WriteLine("Apple");
}
}
public class B : A /* Derived Class */
{
public override void Get_Detail()
{
Console.WriteLine("Bat");
}
}
class Program
{
static void Main(string[] args)
{ /* Calling */
A a = new A();
a.Get_Detail();
B b = new B();
b.Get_Detail();
Console.ReadKey();
}
}
}
In the above program, you can see we are overriding a base class (A) methods and properties which we defined with a virtual keyword in a derived class (B) using override keyword.
Output
Whenever derived class characterizes the same strategy as characterized in its base class, it is known as method overriding It is utilized to accomplish runtime polymorphism. It empowers you to give explicit usage of the technique which is now given by its base class. Overriding methods override a base class technique in a derived class by making a technique with the same name and parameters by utilizing virtual and override keywords.
To perform this task you have to utilize virtual keyword with base class technique and abrogate catchphrase with override keyword with derived class strategy.
So Let's see a simple program which demonstrates method overriding as below.
Programa
using System;
namespace Method_Overriding
{
public class A /* Base Class*/
{
public virtual void Get_Detail()
{
Console.WriteLine("Apple");
}
}
public class B : A /* Derived Class */
{
public override void Get_Detail()
{
Console.WriteLine("Bat");
}
}
class Program
{
static void Main(string[] args)
{ /* Calling */
A a = new A();
a.Get_Detail();
B b = new B();
b.Get_Detail();
Console.ReadKey();
}
}
}
In the above program, you can see we are overriding a base class (A) methods and properties which we defined with a virtual keyword in a derived class (B) using override keyword.
Output
No comments:
Post a Comment