In object-oriented programming (OOP), languages like C# and Java use the override
and new
keywords to modify the behavior of methods in a class and its properties inherited from a parent class (or interface).
However, they are used for different purposes and have unique implications.
override
keywordThe override
keyword is used when we create or override a method in the child class with the same name or signature as in the parent class. This is part of the concept of method overriding, which enables polymorphism to allow objects of the child class to be treated as objects of the parent class while still executing the implementation of the former.
When we use the override
keyword, the method in the child class must have the same name, parameters, and return type as the method in the parent class. This ensures that the method in the child class is the exact replacement of the method in the parent class.
When we want a specialized implementation of the parent class methods in the child class, the methods in the child class completely replace the behavior of the method in the parent class when called with the child class objects.
In the code below, the compiler is confused between the methods of the parent and child classes having the same signatures for the print()
function. Executing the code without method overriding throws a warning, “ChildClass.print()
hides inherited member ParentClass.print()
. Use the new
keyword if hiding was intended.”
using System;class ParentClass{public void print(){Console.WriteLine("This is the method of Parent class");}}class ChildClass:ParentClass{public void print(){Console.WriteLine("This is the method of Child class");}static void Main(){ParentClass pc = new ParentClass(); // Object of ParentClassChildClass cc = new ChildClass(); // Object of ChildClassParentClass pcc = new ChildClass(); // Object of type ParentClass and memory allocated for ChildClasspc.print(); // calls method in ParentClasscc.print(); // calls method in ChildClasspcc.print(); // calls method in ChildClass}}
To deal with the warning, we use the override
keyword with the method of the child class and the virtual
keyword with the method of the parent class. The virtual
keyword with the parent class method tells the compiler that this method is overridden in the child class.
using System;class ParentClass{public virtual void print(){Console.WriteLine("This is the method of Parent class");}}class ChildClass:ParentClass{public override void print(){Console.WriteLine("This is the method of Child class");}static void Main(){ParentClass pc = new ParentClass(); // Object of ParentClassChildClass cc = new ChildClass(); // Object of ChildClassParentClass pcc = new ChildClass(); // Object of type ParentClass and memory allocated for ChildClasspc.print(); // calls method in ParentClasscc.print(); // calls method in ChildClasspcc.print(); // calls method in ChildClass}}
new
keywordThe new
keyword is used for method hiding in C#. This allows defining a method or property in the child class with the same name or signature as in the parent class without the intent to override the method. It hides the method in the parent class, providing a new member with the same name in the child class.
When we create a method with the new
keyword in the child class, it is completely unrelated to the method in the parent class with the same name. This can be confusing because the method of the child class is not called when referencing the child class through a reference to the parent class.
When we execute the code below, we can see that the pcc
object invokes the method of the parent class instead of the child class.
using System;class ParentClass{public void print(){Console.WriteLine("This is the method of Parent class");}}class ChildClass:ParentClass{public new void print(){Console.WriteLine("This is the method of Child class");}static void Main(){ParentClass pc = new ParentClass(); // Object of ParentClassChildClass cc = new ChildClass(); // Object of ChildClassParentClass pcc = new ChildClass(); // Object of type ParentClass and memory allocated for ChildClasspc.print(); // calls method in ParentClasscc.print(); // calls method in ChildClasspcc.print(); // calls method in ChildClass}}
The use of the new
keyword is usually discouraged because it can make the code harder to understand. It should only be used when there is a specific reason to create a member with the same name but different functionality in the child class.
In conclusion, the override
keyword is used to create a method in the child class with the same name and signature. It intends to replace the method with the specialized implementation of a parent class method. The new
keyword is used to create a new method in the child class with the same signatures as in the parent class. It hides the parent class method without the intent to override the method. We should be careful when using these keywords to ensure the behavior and maintainability of the code.
Free Resources