Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • C# - Get Started
  • C# - Version History
  • C# - First Program
  • C# - Keywords
  • C# - Class and Objects
  • C# - Namespace
  • C# - Variables
  • C# - Implicitly-Typed Variables
  • C# - Data Types
  • Numbers
  • Strings
  • DateTime
  • Structure
  • Enum
  • StringBuilder
  • Anonymous Types
  • Dynamic Types
  • Nullable Types
  • C# - Value & Reference Types
  • C# - Interface
  • C# - Operators
  • C# - if else Statements
  • C# - Ternary Operator ?:
  • C# - Switch
  • C# - For Loop
  • C# - While Loop
  • C# - Do-while Loop
  • C# - Partial Class
  • C# - Static
  • C# - Array
  • Multidimensional Array
  • Jagged Array
  • C# - Indexer
  • C# - Generics
  • Generic Constraints
  • C# - Collections
  • ArrayList
  • List
  • SortedList
  • Dictionary
  • Hashtable
  • Stack
  • Queue
  • C# - Tuple
  • C# - ValueTuple
  • C# - Built-in Exceptions
  • Exception Handling
  • throw
  • Custom Exception
  • C# - Delegates
  • Func Delegate
  • Action Delegate
  • Predicate Delegate
  • Anonymous Methods
  • C# - Events
  • C# - Covariance
  • C# - Extension Method
  • C# - Stream I/O
  • C# - File
  • C# - FileInfo
  • C# - Object Initializer
  • OOP - Overview
  • Object-Oriented Programming
  • Abstraction
  • Encapsulation
  • Association & Composition
  • Inheritance
  • Polymorphism
  • Method Overriding
  • Method Hiding
  • C# - Solid Principles
  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle
  • Design Patterns
  • Singleton
  • Abstract Factory
  • Factory Method
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Method Hiding in C#

In the previous chapter, you learned about method overriding using the virtual and override keywords. Here you will learn what will happen if we don't use these keywords and still redefine the base class's methods in the derived class?

Look at the following example.

Example: Override Method without Virtual and Override Keywords
class Person
{
    public virtual void Greet()
    {
        Console.WriteLine("Hi! I am a person.");
    }
}

class Employee : Person
{
    public override void Greet()
    {
        Console.WriteLine("Hello! I am an employee.");
    }
}

As you can see, the Greet() method is defined in the base class as well as the derived class without the virtual and override keywords. Now, guess the output of the following program.

Example: Method Hiding
class Program
{
    public static void Main()
    {
        Person p1 = new Person();
        p1.Greet(); 
            
        Person p2 = new Employee();
        P2.Greet();

        Employee emp = new Employee();
        emp.Greet();
    }
}
Try it

When you compile the above program, it will give the following warning.

warning CS0108: 'Employee.Greet()' hides inherited member 'Person.Greet()'. 
Use the new keyword if hiding was intended.

However, the above program will successfully run and will give the following output.

Output:
Hi! I am a person. Hello! I am an employee. Hello! I am an employee.

As you can see, the calling of the Greet() method does not depend on the type of object. Here, Employee.Greet() hides the base class method Person.Greet(). This is called method hiding (of base class) in C#.

Method Hiding using new Keyword

Use the new keyword in the derived class to hide the base class method. This will be useful when you are resuing third-party APIs where you don't have control over the base class.

The new keyword will not give the above warning. The following will give the same result but will not give any warning at compile time.

Example: new Keyword
class Person
{
    public void Greet()
    {
        Console.WriteLine("I am a person!");
    }
}

class Employee : Person
{
    public new void Greet()
    {
        Console.WriteLine("I am the Manager!");
    }
}
Try it

By using the new keyword with the derived class method, C# treats it as a new method of the derived class and hides the base class method. The Employee.Greet() will be treated as a new method.

The new keyword preserves the relationships that produce that output, but it suppresses the warning. The variables of type base class continue to access the members of the base class, and the variable of type derived class continues to access members in the derived class first, and then consider members inherited from the base class.

Learn when to use override and new keywords.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.