You will learn followings:
- C# | Method Overriding
- C# | Method Overloading
C# | Method Overriding
Method overrides in C# are similar to virtual functions in C++. Method overriding is a technique that allows you to call a function from another class (the base class) in a derived class. Creating a method in a derived class that has the same signature as a method in the base class is called method overriding.
Simply put, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. A subclass method is said to override a superclass method if the subclass method has the same name, parameters or signature, and return type (or subtype) as the superclass method. class. Method overriding is one of the ways C# achieves runtime polymorphism (dynamic polymorphism).
A method that is overridden by an override declaration is called an overridden base method. An override method is a new implementation of a member inherited from a base class. The base method being overridden must be a virtual method, an abstract method, or be overridden.

Example:
class base_class
{
public void test();
}
class derived_class : base_class
{
public void test();
}
class Main_Method
{
static void Main()
{
derived_class d = new derived_class();
d.test();
}
}
Here
Here the base class is inherited in the derived class and the method test() which has the same signature in both the classes, is overridden.
In C# we can use 3 types of keywords for Method Overriding:
- virtual keyword: This modifier or keyword use within base class method. It is used to modify a method in base class for overridden that particular method in the derived class.
- override: This modifier or keyword use with derived class method. It is used to modify a virtual or abstract method into derived class which presents in base class.
- base Keyword: This is used to access members of the base class from derived class. It is explained in detail little below in this post.
class base_class
{
public virtual void test();
}
class derived_class : base_class
{
public override void test();
}
class Main_Method
{
static void Main()
{
derived_class d = new derived_class();
d.test();
base_class b = new derived_class();
b.test();
}
}
Here first, d refers to the object of the class derived_class and it invokes test() of the class derived_class then, b refers to the reference of the class base and it hold the object of class derived and it invokes test() of the class derived. Here test() method takes permission from base class to overriding the method in derived class.
Example 1: Method Overriding without using virtual and override modifiers
// C# program to demonstrate the method overriding
// without using 'virtual' and 'override' modifiers
using System;
// base class name 'baseClass'
class baseClass
{
public void show()
{
Console.WriteLine("Base class");
}
}
// derived class name 'derived'
// 'baseClass' inherit here
class derived : baseClass
{
// overriding
new public void show()
{
Console.WriteLine("Derived class");
}
}
class CodeConfigs {
// Main Method
public static void Main()
{
// 'obj' is the object of
// class 'baseClass'
baseClass obj = new baseClass();
// invokes the method 'show()'
// of class 'baseClass'
obj.show();
obj = new derived();
// it will invokes the method
// 'show()' of class 'baseClass'
obj.show();
}
}
Output:
Base class
Base class
Explanation: In this program, the object obj invokes class baseClass two times and call the method show() of class baseClass. To avoid this problem, we use virtual and override keyword.
Example 2: Method overriding using virtual and override modifiers.
// C# program to illustrate the use of
//'virtual' and 'override' modifiers
using System;
class baseClass {
// show() is 'virtual' here
public virtual void show()
{
Console.WriteLine("Base class");
}
}
// class 'baseClass' inherit
// class 'derived'
class derived : baseClass
{
//'show()' is 'override' here
public override void show()
{
Console.WriteLine("Derived class");
}
}
class CodeConfigs{
// Main Method
public static void Main()
{
baseClass obj;
// 'obj' is the object
// of class 'baseClass'
obj = new baseClass();
// it invokes 'show()'
// of class 'baseClass'
obj.show();
// the same object 'obj' is now
// the object of class 'derived'
obj = new derived();
// it invokes 'show()' of class 'derived'
// 'show()' of class 'derived' is overridden
// for 'override' modifier
obj.show();
}
}
Output:
Base class
Derived class
base Keyword:
This is used to access members of the base class from derived class. It basically used to access constructors and methods or functions of the base class. The base keyword cannot use within a static method. Base keyword specifies which constructor of the base class should be invoked while creating the instances of the derived class.
Use of Base keyword:
- Call methods or functions of base class from derived class.
- Call constructor internally of base class at the time of inheritance.
Example 3:
// C# program to show the use of 'base'
// keyword in method overriding
using System;
// base class
public class web {
string name = "CodeConfig";
// 'showdata()' is member method,
// declare as virtual
public virtual void showdata()
{
Console.WriteLine("Website Name: " + name);
}
}
// derived class
// class 'web' is inherits
// class 'stream'
class stream : web {
string s = "Data-Science";
//'showdata()' is overridden
// in derived class
public override void showdata()
{
// Calling 'showdata()' of base
// class using 'base' keyword
base.showdata();
Console.WriteLine("About: " + s);
}
}
class CodeConfigs {
// Main Method
static void Main()
{
// 'E' is object of class stream
// also works as object of
// class 'web'
stream E = new stream();
// it first invokes 'showdata()'
// of class 'web' then it invokes
// 'showdata()' of class 'stream'
E.showdata();
}
}
Output:
Website Name: CodeConfig
About: Data-Science
Example 4: How the base keyword specifies the calling of base-class constructor from derived class when derived class instances are created.
// C# program to show how base keyword
// specifies the calling of base-class
// constructor from the derived class
// when derived class instances are created
using System;
// base class
public class A {
int n1, n2;
// default constructor
public A()
{
Console.WriteLine("Default Constructor Invoked");
}
// parameterized constructor
public A(int i, int j)
{
// construct values
n1 = i;
n2 = j;
Console.WriteLine("Parameterized Constructor Invoked");
Console.WriteLine("Invoked Values are: " + n1 + " and " + n2);
}
}
// derived class
public class DerivedClass :A
{
// This constructor will instantiate
// 'A()' [no argument constructor]
// using 'base' keyword
public DerivedClass() : base() { }
// This constructor will instantiate
// 'A(int i, int j)' [parameterized
// constructor] using 'base' keyword
public DerivedClass(int i, int j) : base(i, j) { }
// Main Method
static void Main()
{
// invoke no argument constructor
DerivedClass d1 = new DerivedClass();
Console.WriteLine();
// invoke parameterized constructor
DerivedClass d2 = new DerivedClass(10, 20);
}
}
Output:
Default Constructor Invoked
Parameterized Constructor Invoked
Invoked Values are: 10 and 20
Example 5: It shows how base keyword specifies the base-class constructor called from derived class and also calling of a method using the base keyword from the derived class.
// C# program to show how 'base' keyword specifies
// the base-class constructor that called from
// derived class and also calling a method 'swap'
// from derived class using base keyword
using System;
// base class
public class A {
public int n1, n2;
// default constructor
public A()
{
Console.WriteLine("In clssA 'no argument constructor' invoked");
}
// parameterized constructor
public A(int i, int j)
{
// construct values
n1 = i;
n2 = j;
Console.WriteLine("in class A 'parameterized constructor' invoked");
Console.WriteLine("the invoked values are " + n1 + " and " + n2);
Console.WriteLine();
}
public virtual void swap()
{
Console.WriteLine("swap function of base class(A) invoked");
Console.WriteLine("Before swap num1 = {0} and num2 = {1}", n1, n2);
// swapping
int t = n1;
n1 = n2;
n2 = t;
Console.WriteLine("After swap num1 = {0} and num2 = {1}", n1, n2);
}
}
// derived class
public class DerivedClass : A {
// This constructor will instantiate
// 'A' [no argument constructor]
// using 'base' keyword
public DerivedClass() : base() { }
// This constructor will instantiate
// 'A' [parameterized constructor]
// using 'base' keyword
public DerivedClass(int i, int j) : base(i, j) { }
public override void swap()
{
// it access the swap function of
// 'A' using 'base' keyword
base.swap();
Console.WriteLine();
Console.WriteLine("Swap function of derived class invoked");
Console.WriteLine("Before swap num1 = {0} and num2 = {1}", n1, n2);
// swapping
int t = n1;
n1 = n2;
n2 = t;
Console.WriteLine("After swap num1 = {0} and num2 = {1}", n1, n2);
}
// Main Method
static void Main()
{
// invoke no argument constructor
DerivedClass d1 = new DerivedClass();
Console.WriteLine();
// invoke parameterized constructor
DerivedClass d2 = new DerivedClass(10, 20);
// calling swap function
d2.swap();
}
}
Note:
- Method overriding is possible only in derived classes. Because a method is overridden in the derived class from the base class.
- A non-virtual or a static method can’t be overridden.
- Both the override method and the virtual method must have the same access level modifier.
I hope you enjoyed reading this post. For more information you can check out Microsoft learn.
For more information you can visit our C# Section for more such posts.
C# | Method Overloading
Method overloading is a common way to implement polymorphism. This is the ability to redefine functions in multiple formats. Users can implement function overloading by defining two or more functions within a class with the same name. C# can distinguish between methods with different method signatures. That is, within the same class, methods may have the same name but different parameter lists (that is, number of parameters, order of parameters, and data types of parameters).
- Overloaded methods are differentiated based on the number and type of the parameters passed as arguments to the methods.
- You cannot define more than one method with the same name, Order and the type of the arguments. It would be compiler error.
- The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.
Why do we need Method Overloading?
When you need to perform the same type of operation in different ways, i.e.in response to different inputs. The examples described below perform addition operations on various inputs. It's difficult to give a single action many different meaningful names.
Different ways of doing overloading methods-
Method overloading can be done by changing:
- The number of parameters in two methods.
- The data types of the parameters of methods.
- The Order of the parameters of methods.
By changing the Number of Parameters
/*
C# program to demonstrate the function
overloading by changing the Number
of parameters */
using System;
class CodeConfigs {
// adding two integer values.
public int Add(int a, int b)
{
int sum = a + b;
return sum;
}
// adding three integer values.
public int Add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
CodeConfigs ob = new CodeConfigs();
int sum1 = ob.Add(1, 2);
Console.WriteLine("sum of the two "
+ "integer value : " + sum1);
int sum2 = ob.Add(1, 2, 3);
Console.WriteLine("sum of the three "
+ "integer value : " + sum2);
}
}
Output:
sum of the two integer value: 3
sum of the three integer value: 6
By changing the Data types of the parameters
/*
C# program to demonstrate the function
overloading by changing the Data types
of the parameters
*/
using System;
class CodeConfigs {
// adding three integer values.
public int Add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
// adding three double values.
public double Add(double a,
double b, double c)
{
double sum = a + b + c;
return sum;
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
CodeConfigs ob = new CodeConfigs();
int sum2 = ob.Add(1, 2, 3);
Console.WriteLine("sum of the three "
+ "integer value : " + sum2);
double sum3 = ob.Add(1.0, 2.0, 3.0);
Console.WriteLine("sum of the three "
+ "double value : " + sum3);
}
}
Output:
sum of the three-integer value: 6
sum of the three double value: 6
By changing the Order of the parameters
/*
C# program to demonstrate the function
overloading by changing the
Order of the parameters
*/
using System;
class CodeConfigs{
// Method
public void Identity(String name, int id)
{
Console.WriteLine("Name1 : " + name + ", "
+ "Id1 : " + id);
}
// Method
public void Identity(int id, String name)
{
Console.WriteLine("Name2 : " + name + ", "
+ "Id2 : " + id);
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
CodeConfigs obj = new CodeConfigs();
obj.Identity("Aman", 1);
obj.Identity(2, "Naman");
}
}
Output:
Name1 : Aman, Id1 : 1
Name2 : Naman, Id2 : 2
What happens when method signature is same, and the return type is different?
The compiler throws an error because the return value alone is not enough to determine which function should be called. Method overloading is possible only if both methods have different types of parameters (that is, different signatures).
Example:
// C# program to show error when method signature
// is the same and the return type is different.
using System;
class CodeConfigs
{
// adding two integer value.
public int Add(int a, int b)
{
int sum = a + b;
return sum;
}
// adding three integer value.
public double Add(int a, int b)
{
double sum = a + b + 0.0;
return sum;
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
CodeConfigs ob = new CodeConfigs();
int sum1 = ob.Add(1, 2);
Console.WriteLine("sum of the two "
+ "integer value :" + sum1);
int sum2 = ob.Add(1, 2);
Console.WriteLine("sum of the three "
+ "integer value :" + sum2);
}
}
I hope you enjoyed reading this post. For more information you can check out Microsoft learn.
For more information you can visit our C# Section for more such posts.
No comments:
Post a Comment