In Method Hiding, you can completely hide the implementation of the methods of a base class from the derived class using the new keyword.
Method Overriding and Method Hiding are two different concepts related to polymorphism in object-oriented programming. Let's explore the differences between them with examples:
Method Overriding: Method overriding allows a derived class to provide a specific implementation for a method that is already defined in its base class. When a method is overridden, the version of the method in the derived class is called at runtime instead of the version in the base class.
To achieve method overriding, the method in the base class must be marked as virtual
, and the method in the derived class must be marked as override
. The method signature (name, return type, and parameters) must be the same in both the base and derived classes.
Example of Method Overriding:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a generic shape.");
}
}
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle.");
}
}
public class Program
{
public static void Main(string[] args)
{
Shape shape = new Circle();
shape.Draw(); // Output: "Drawing a circle."
}
}
In this example, we have a base class Shape
with a virtual method Draw()
. The derived class Circle
overrides the Draw()
method with its own specific implementation. When we create an instance of Circle
and call the Draw()
method using a reference of the base class (Shape
), the overridden method in the Circle
class is executed.
Method Hiding: Method hiding, also known as method shadowing, allows a derived class to define a new method with the same name as a method in its base class. However, this new method does not participate in polymorphism; instead, it simply hides the method with the same name in the base class.
To achieve method hiding, the method in the base class must be marked as static
, and the method in the derived class must use the new
keyword.
Example of Method Hiding:
public class Shape
{
public static void Draw()
{
Console.WriteLine("Drawing a generic shape.");
}
}
public class Circle : Shape
{
public new static void Draw()
{
Console.WriteLine("Drawing a circle.");
}
}
public class Program
{
public static void Main(string[] args)
{
Shape shape = new Circle();
shape.Draw(); // Output: "Drawing a generic shape."
}
}
In this example, the Draw()
method in the base class Shape
is marked as static
. The derived class Circle
defines its own Draw()
method, also marked as static
and using the new
keyword. When we call the Draw()
method using a reference of the base class (Shape
), the method in the base class is called because method hiding does not participate in polymorphism.
In summary, method overriding allows derived classes to provide a specific implementation for a method defined in the base class, while method hiding allows derived classes to define a new method that hides a method with the same name in the base class. Method overriding involves the virtual
and override
keywords, while method hiding involves the static
and new
keywords.