Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1102411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:10:57+00:00 2026-05-17T01:10:57+00:00

I am a bit confused about overriding vs. hiding a method in C#. Practical

  • 0

I am a bit confused about overriding vs. hiding a method in C#. Practical uses of each would also be appreciated, as well as an explanation for when one would use each.

I am confused about overriding – why do we override? What I have learnt so far is that by overring we can provide desired implementation to a method of a derived class, without changing the signature.

If I don’t override the method of the superclass and I make changes to the method in the sub class, will that make changes to the super class method ?

I am also confused about the following – what does this demonstrate?

class A
{
    virtual m1()
    {
        console.writeline("Bye to all");
    }
}

class B : A
{
    override m1()
    {
        console.writeLine("Hi to all");
    }
}

class C
{
    A a = new A();
    B b = new B();
    a = b; (what is this)
    a.m1(); // what this will print and why?

    b = a; // what happens here?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:10:58+00:00Added an answer on May 17, 2026 at 1:10 am

    Consider:

    public class BaseClass
    {
      public void WriteNum()
      {
        Console.WriteLine(12);
      }
      public virtual void WriteStr()
      {
        Console.WriteLine("abc");
      }
    }
    
    public class DerivedClass : BaseClass
    {
      public new void WriteNum()
      {
        Console.WriteLine(42);
      }
      public override void WriteStr()
      {
        Console.WriteLine("xyz");
      }
    }
    /* ... */
    BaseClass isReallyBase = new BaseClass();
    BaseClass isReallyDerived = new DerivedClass();
    DerivedClass isClearlyDerived = new DerivedClass();
    
    isReallyBase.WriteNum(); // writes 12
    isReallyBase.WriteStr(); // writes abc
    isReallyDerived.WriteNum(); // writes 12
    isReallyDerived.WriteStr(); // writes xyz
    isClearlyDerived.WriteNum(); // writes 42
    isClearlyDerived.writeStr(); // writes xyz
    

    Overriding is the classic OO way in which a derived class can have more specific behaviour than a base class (in some languages you’ve no choice but to do so). When a virtual method is called on an object, then the most derived version of the method is called. Hence even though we are dealing with isReallyDerived as a BaseClass then functionality defined in DerivedClass is used.

    Hiding means that we have a completely different method. When we call WriteNum() on isReallyDerived then there’s no way of knowing that there is a different WriteNum() on DerivedClass so it isn’t called. It can only be called when we are dealing with the object as a DerivedClass.

    Most of the time hiding is bad. Generally, either you should have a method as virtual if its likely to be changed in a derived class, and override it in the derived class. There are however two things it is useful for:

    1. Forward compatibility. If DerivedClass had a DoStuff() method, and then later on BaseClass was changed to add a DoStuff() method, (remember that they may be written by different people and exist in different assemblies) then a ban on member hiding would have suddenly made DerivedClass buggy without it changing. Also, if the new DoStuff() on BaseClass was virtual, then automatically making that on DerivedClass an override of it could lead to the pre-existing method being called when it shouldn’t. Hence it’s good that hiding is the default (we use new to make it clear we definitely want to hide, but leaving it out hides and emits a warning on compilation).

    2. Poor-man’s covariance. Consider a Clone() method on BaseClass that returns a new BaseClass that’s a copy of that created. In the override on DerivedClass this will create a DerivedClass but return it as a BaseClass, which isn’t as useful. What we could do is to have a virtual protected CreateClone() that is overridden. In BaseClass we have a Clone() that returns the result of this – and all is well – in DerivedClass we hide this with a new Clone() that returns a DerivedClass. Calling Clone() on BaseClass will always return a BaseClass reference, which will be a BaseClass value or a DerivedClass value as appropriate. Calling Clone() on DerivedClass will return a DerivedClass value, which is what we’d want in that context. There are other variants of this principle, however it should be noted that they are all pretty rare.

    An important thing to note with the second case, is that we’ve used hiding precisely to remove surprises to the calling code, as the person using DerivedClass might reasonably expect its Clone() to return a DerivedClass. The results of any of the ways it could be called are kept consistent with each other. Most cases of hiding risk introducing surprises, which is why they are generally frowned upon. This one is justified precisely because it solves the very problem that hiding often introduces.

    In all, hiding is sometimes necessary, infrequently useful, but generally bad, so be very wary of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.