In this article we will see the differences between the two with an example.
In my project, I created a ParentClass with a method named “ParentMethod”. I have also created a child class named “ChildClass” that is inheriting from the parent class.
- using System;
- namespace MethodOverridingMethodHiding {
- class ParentClass {
- public void ParentMethod() {
- Console.WriteLine("I am a parent method");
- }
- }
- class ChildClass : ParentClass {
- }
- }
To override a base class method, we need to make that method “virtual”.
- class ParentClass {
- public virtual void ParentMethod() {
- Console.WriteLine("I am a parent method");
- }
- }

It will generate the following code.
- class ChildClass : ParentClass {
- public override void ParentMethod() {
- base.ParentMethod();
- }
- }
- class ChildClass : ParentClass {
- public override void ParentMethod() {
- Console.WriteLine("I am overriding the parent class method");
- }
- }
- class Program {
- static void Main(string[] args) {
- ParentClass pc = new ChildClass();
- pc.ParentMethod();
- }
- }

Now let's look at an example where we will hide the base method rather than overriding it.
First, we need to remove the virtual keyword from the ParentMethod.
- class ParentClass {
- public void ParentMethod() {
- Console.WriteLine("I am a parent method");
- }
- }
- class ChildClass : ParentClass {
- public new void ParentMethod() {
- Console.WriteLine("I am overriding the parent class method");
- }
- }
- class Program {
- static void Main(string[] args) {
- ParentClass pc = new ChildClass();
- pc.ParentMethod();
- }
- }
The hidden parent class method will be invoked.

Note
But if you want to invoke the child class method, we can do the following in the main method:
- class Program {
- static void Main(string[] args) {
- ChildClass cc = new ChildClass();
- cc.ParentMethod();
- }
- }

From the preceding two examples it is very clear that in method overriding, a base class reference variable pointing to a child class object will invoke the overridden method in the child class and in method hiding, a base class reference variable pointing to a child class object will invoke the hidden method in the base class.
Overriding
Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.
Example
- public class BaseClass
- {
- public virtual string GetMethodOwnerName()
- {
- return “Base Class”;
- }
- }
- public class ChildClass : BaseClass
- {
- public override string GetMethodOwnerName()
- {
- return “Child Class”;
- }
- }
- Methods have a different return type
- Methods have a different access modifier
- Methods have a different parameter type or order
- Methods are non virtual or static
A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.
Example
- Public class BaseClass
- {
- public string GetMethodOwnerName()
- {
- return "Base Class";
- }
- }
- public class ChildClass : BaseClass
- {
- public new string GetMethodOwnerName()
- {
- return "ChildClass";
- }
- }
- static void Main(string[] args)
- {
- ChildClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }

If we do not use the new keyword the compiler generates the warning:

Mixing Method (Overriding and shadowing (Method Hiding))
We can also use shadowing and method overriding together using the virtual and new keywords. This is useful when we want to further override a method of the child (derived) class.
Example
- public class BaseClass
- {
- public virtual string GetMethodOwnerName()
- {
- return "Base Class";
- }
- }
- public class ChildClass : BaseClass
- {
- public new virtual string GetMethodOwnerName()
- {
- return "ChildClass";
- }
- }
- public class SecondChild : ChildClass
- {
- public override virtual string GetMethodOwnerName()
- {
- return "Second level Child";
- }
- }
Shadowing | Overriding |
Shadowing is a VB.Net concept. It also known as method hiding in C#. Using this concept we can provide a new implementation for the base class method without overriding it. | Overriding allows us to re-write a base class function with a different definition. |
Using the “new” keyword we can do the shadowing or method hiding. | C# uses the virtual/abstract and override keyword for method overriding. |
Shadowing redefines an entire method or function. | Overriding redefines only the implementation of a method or function. |
Showing is used to protect against subsequent base class modification. | Overriding does polymorphism by defining a different implementation. |
We can change the access modifier. | We cannot change the access modifier. The access modifier must be the same as in the base class method or function. |
There is no control of a base class on shadowing. In other words, a base class element cannot enforce or stop shadowing. | The base class has some control over the overriding. Using the keyword abstract, the base class forces the child (derived) class to implement the function or method. |
Shadowing an element (function method or property) can be inherited further in a child (derived) class. The shadowed element is still hidden. | The same as shadowing, overriding an element is inherited further in a derived class and the overridden element is still overridden. |
In shadowing, the signature of an element could be different. | In overriding, the signature of the element must be the same. |
In shadowing, the base class cannot access the newly created child (derived) class method. This is because the base class has the same name of the element. |
In concept, the base class can be accessed using the child object's overridden method.
|
- static void Main(string[] args)
- {
- BaseClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }

In overriding, the base class can be accessed using the child object's overridden method.
Overriding Example
- static void Main(string[] args)
- {
- BaseClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }

We cannot use the new and override keywords together. If you do then the compiler throws a compilation error.
Output

Methods Overloading Vs Method Overriding Vs Method Hiding in C#
IntroductionMethod overloading, method overriding and method hiding are very beautiful features in C# but at the same time they are very confusing for beginners. This article is for beginners to understand these concepts.
Methods Overloading
The method overloading feature in C# is very helpful in code reusability by creating a different version of a method, meaning method overloading is nothing but a technique to create two or more methods of the same name but different signatures.
Same name but different signatures
Method / Function overloading is multiple methods in a class with the same name but different signatures.
The following is a C# method:
- public static void myMethod(int a, int b)
- {
- Console.WriteLine("myMethod Version 1 is printed");
- }
A
method signature indicates the number of parameters, types of
parameters or kinds of method parameters used for creating an overloaded
version of a method. An important point is the return type of the
method is not includeed in the signature, meaning if we try to create
the method with the same but different return type then we will get a
complier error message.
Here
in the following we can see that the 5 versions of various methods are
available with the same name but different signatures.
- // Number of parameter
- public static void myMethod(int a, int b, int c)
- {
- Console.WriteLine("myMethod Version 2 is printed");
- }
- // Type of parameters
- public static void myMethod(float a, float b, float c)
- {
- Console.WriteLine("myMethod Version 3 is printed");
- }
- public static void myMethod(float a, int b, float c)
- {
- Console.WriteLine("myMethod Version 4 is printed");
- }
- // Method parameter
- public static void myMethod(float a, out int b, float c)
- {
- Console.WriteLine("b={0} , myMethod Version 5 is printed");
- b =(int)(a + c);
- }

Sample Code
Method Overriding
Method
overriding is nothing but a feature by which a derived class can
provide a new implementation for the base class method having the same
name and signature as in a derived class. A derived class method can be
overridden using the keyword override and that must be virtual or
abstract in the base class.- namespace MethodOverloading
- {
- class Program
- {
- static void Main(string[] args)
- {
- myMethod(4,4);
- }
- public static void myMethod(int a, int b)
- {
- Console.WriteLine("myMethod Version 1 is printed");
- }
- // Number of parameter
- public static void myMethod(int a, int b, int c)
- {
- Console.WriteLine("myMethod Version 2 is printed");
- }
- // Type of parameters
- public static void myMethod(float a, float b, float c)
- {
- Console.WriteLine("myMethod Version 3 is printed");
- }
- public static void myMethod(float a, int b, float c)
- {
- Console.WriteLine("myMethod Version 4 is printed");
- }
- // Method parameter
- public static void myMethod(float a, out int b, float c)
- {
- Console.WriteLine("b={0} , myMethod Version 5 is printed");
- b =(int)(a + c);
- }
- }
Base Class
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Maruti : cars
- {
- public override void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Maruti");
- }
- }
Sample Code
- namespace MethodOverriding
- {
- class Program
- {
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Maruti : cars
- {
- public override void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Maruti");
- }
- }
- static void Main(string[] args)
- {
- cars car = new Maruti();
- car.displayBrand();
- Console.ReadLine();
- }
- }
Method
hiding is nothing but invoking the hidden base class method when the
base class variable reference is pointing to the derived class object.
This can be done by the new keyword in the derived class method implementation, in other words the derived class has the same method with the same name and signature.
Base Class
Derived Class
This can be done by the new keyword in the derived class method implementation, in other words the derived class has the same method with the same name and signature.
Base Class
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Honda : cars
- {
- public new void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Honda");
- }
- }
Here
we can see the base class and derived class with the same
displayBrand() method name and signature. So whenever a base class
variable is pointing to a derived class and the method is called in a
normal scenario, it will decide the method call from the derived class
at runtime.
But the new keyword will help to invoke the base class version of the method displayBrand(); this is called method hiding.
But the new keyword will help to invoke the base class version of the method displayBrand(); this is called method hiding.
- cars car = new Maruti();
- car.displayBrand();
- namespace MethodHiding
- {
- class Program
- {
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Honda : cars
- {
- public new void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Honda");
- }
- }
- static void Main(string[] args)
- {
- cars car = new Maruti();
- car.displayBrand();
- Console.ReadLine();
- }
- }
- In method overloading, the method with the name and signature but different return type will provide a complier error and is not allowed in C#.
- Method overriding is only possible in derived classes.
- To override a base class method in a derived class with the same name and signature, the base class method should be virtual or abstract.
- Method hiding will be done using the new keyword in the derived class for virtual methods in the base class.
No comments:
Post a Comment