Skip to main content

method hiding

*■ 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

Popular posts from this blog

Raster scan Vs Vector Scan

1. Raster Scan Display   How It Works : A raster scan display works by painting an image on the screen pixel by pixel, row by row. It follows a systematic pattern where the electron beam (in CRT monitors) or the display elements (in modern LCD/LED screens) sweep across the screen from left to right, top to bottom, in a series of horizontal lines (scan lines). This process is akin to how a traditional TV screen works.   Process : The display draws the image starting from the top-left corner, moving to the right, then moves to the next row below, and repeats this process until the entire screen is filled. This pattern creates a grid of pixels, where each pixel can have a color and brightness level.   Characteristics : Pixel-based : The screen consists of a grid of pixels, and each pixel can have a distinct color and intensity. Continuous Image : Raster scan displays are capable of displaying detailed and complex images, including photographs and videos, because they break t...

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

unit -1 Introduction of Image processing

  What is Image Processing? Image processing is a method to perform operations on an image to enhance it or extract useful information. It is a type of signal processing where the input is an image, and the output may be either an image or characteristics/features associated with that image. Goals of Image Processing Image Enhancement : Improving visual appearance (e.g., contrast, sharpness) Image Restoration : Removing noise or distortion Image Compression : Reducing the amount of data required to represent an image Feature Extraction : Identifying objects, edges, or patterns Image Analysis : Understanding and interpreting image content Object Recognition : Detecting and identifying objects in an image What is an Image? An image is a two-dimensional function f(x, y) , where x and y are spatial coordinates, and f is the intensity (brightness or color) at that point. For digital images, both x, y, and f are finite and discrete. Types of Image Representation...