Advertisement
//C# A simple example of method overriding
using System;
//Base class
class A
{
//Defining a method GetName() in class A with virtual keyword
//It will display the name of its class i.e. A
public virtual void GetName()
{
Console.WriteLine("A");
}
}
//Derived class of A
class B:A
{
//Overriding the method GetName() of class A in class B, with override keyword
//This overridden version will display the name of its class i.e. B
public override void GetName()
{
Console.WriteLine("B");
}
public static void Main(String[] ar)
{
//Creating an object of class B
B ob = new B();
//Calling the overridden method
//by using the object of class B
ob.GetName();
}
}
B
//C# An Example of method overriding
//The return type of the overriding method
//must match with the return type of the method being overridden.
using System;
//Base class
class A
{
//The method ReturnObject() has return type A
//and it is marked with a virtual keyword
public virtual A ReturnObject()
{
return new A();
}
}
//class B is inheriting class A i.e. derived class
class B:A
{
//Overriding the method ReturnObject() of class A in class B, with override keyword
//This overridden version also has the matching return type A
public override A ReturnObject()
{
return new B();
}
public static void Main(String[] ar)
{
//Creating and object of class B
B ob = new B();
//Calling the overridden version of derived class, B
//By using its object.
Console.WriteLine(ob.ReturnObject());
}
}
B
Advertisement
//C# Example of method overriding
using System;
//Base class
class Maths
{
//Instance variables
public int num1, num2;
//A method performing addition operation on two integers
//It has two int arguments
//And, it is declared with virtual keyword
public virtual int MathOperation(int a, int b)
{
num1 = a;
num2 = b;
return(num1+num2);
}
}
//Maths class is inherited by Subtract class i.e. Derived class
class Subtract:Maths
{
//Instance variables
public int x, y;
//The MathOperation() of base class is overridden to perform subtract operation on two integers.
//It also has two int arguments
//And, we have used the override modifier with it
public override int MathOperation(int a, int b)
{
x = a;
y = b;
return (x-y);
}
}
class Override
{
public static void Main(String[] ar)
{
//Creating and object of derived class, Subtract
Subtract ob = new Subtract();
//Calling the overriding method of class Subtract
//By using its object
Console.WriteLine(ob.MathOperation(5,2));
}
}
3
//C# Example of method overriding
//The access modifier of an overriding method must match
//with the access modifier of a virtual method being overridden.
using System;
//Base class
class A
{
//The method ShowName() has "public" access
//It is defined with virtual keyword
public virtual void ShowName()
{
Console.WriteLine("A");
}
}
//Derived class
class B:A
{
//The overriding method ShowName() also has a "public" access
public override void ShowName()
{
Console.WriteLine("B");
}
public static void Main(String[] ar)
{
//Creating an object of derived class B
B ob = new B();
//Calling the overriding method of class B
//by using its object
ob.ShowName();
}
}
B
//C# You cannot use the virtual keyword with a private method
//or even an abstract method, which is meant to be defined by the derived class
using System;
class A
{
//The ShowMethod() has private access and it cannot be overridden.
//Therefore, the virtual keyword cannot be used with it.
private virtual void ShowMethod()
{
Console.WriteLine("A");
}
}
A.cs(8,22): error CS0621: 'A.ShowMethod()': virtual or abstract members cannot be private
//C# You can use the virtual keyword with a protected method
using System;
//Base class
class A
{
//The method ShowName() has "protected" access
//It is defined with virtual keyword
protected virtual void ShowName()
{
Console.WriteLine("A");
}
}
//Derived class
class B:A
{
//The overriding method ShowName() also has a "protected" access
protected override void ShowName()
{
Console.WriteLine("B");
}
public static void Main(String[] ar)
{
//Creating an object of derived class B
B ob = new B();
//Calling the overriding method of class B
//by using its object
ob.ShowName();
}
}
B
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement