*■ Method Hiding*
• Method hiding in C#, also known as method shadowing.
• It is an approach of re-implementing the parent class method under the child class with the same name.
• Method hiding is a technique that allows a derived class to hide the methods of a base class. This is done by defining a new method in the derived class with the same name as a method in the base class, but using the new keyword.
• The derived class method must be marked with 'new' keyword.
• The derived class method can have a different return type or method signature than the base class method. But it’s generally a good practice to keep the signature the same to avoid confusion.
• Method hiding does not participate in polymorphism. That is, if you have a base class reference pointing to a derived class object, it will call the base class method if the method is hidden, regardless of the derived class method’s implementation. Only methods that are overridden using the override keyword participate in polymorphism
*Example* :-
1)using new keyword
class BaseClass
{
public void Display()
{
Console.WriteLine("BaseClass Display");
}
}
class DerivedClass : BaseClass
{
public new void Display() // Method hiding
{
Console.WriteLine("DerivedClass Display");
}
}
class Program
{
static void Main()
{
BaseClass baseObj = new BaseClass();
DerivedClass derivedObj = new DerivedClass();
BaseClass baseRefToDerived = new DerivedClass();
baseObj.Display(); // Output: BaseClass Display
derivedObj.Display(); // Output: DerivedClass Display
baseRefToDerived.Display(); // Output: BaseClass Display
}
}
2)without using the new keyword
If you don't use the new keyword in derived class then it will still produce same output as above but will give a warning message as below
'DerivedClass.Display()' hides inherited member 'BaseClass.Display()' Use the new keyword if hiding was intended
➤ How to call the hidden method
In method hiding, you can also call the hidden method of the base class in the derived class using below different ways:
1) By using the base keyword ->
base.parent_method_name()
2) use the base class reference variable ponting to a derived classic object(instance)
BaseClass obj = new DerivedClass();
obj.Display(); // this will call the base class Display() method
___________________________________
*➤ Difference b/w Method Overriding and method Hiding*
1) Purpose
➔ Allows a derived class to provide a specific implementation of a method that is already defined in its base class.
➔ Allows a derived class to define a method with the same name as a method in the base class, but without affecting the base class method
2) Usage
➔ Use the 'virtual' keyword in the base class method and the 'override' keyword in the derived class method.
➔ Use the 'new' keyword in the derived class method to hide the base class method.
3) Behaviour
➔ When you call the method on an instance of the derived class, the derived class's version of the method is executed.
➔ When you call the method on an instance of the derived class, the derived class's version is executed. However, if the method is called on a reference of the base class type, the base class's version is used.
4) Polymorphism
➔ overriding Supports runtime polymorphism
➔Method Hiding does supports compile time polymorphism
5) Access modifiers
➔ Overriding: The access level of the overridden method in the derived class must be the same or less restrictive than the method in the base class.
➔ Method Hiding: The access level of the hiding method can be different from the base class method.
6) Inheritance Chain
➔ If a class overrides a method, any further derived classes can also override the method, creating a chain of overridden methods.
➔ Hiding does not propagate in an inheritance chain. Each derived class that hides a method does so independently.
7) Method signature
➔ For overriding, the method signature in the derived class must exactly match the method signature in the base class.
➔ The method signature in the derived class can match the base class method signature or be different.
8) Base class access
➔ You can call the base class's version of the method from the overridden method using base.MethodName().
➔ You cannot directly call the hidden base class method from the derived class method using base.MethodName(). You would need to explicitly cast to the base class.
Comments
Post a Comment