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 6156127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:33:22+00:00 2026-05-23T20:33:22+00:00

Given the following: class BC { public void Display() { System.Console.WriteLine(BC::Display); } } class

  • 0

Given the following:

class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    

     b = new DC();
     b.Display();    
  }
}

I understand that the following code calls the base class Display() method:

BC b;
b = new BC();
b.Display();  

And that the following lines call the derived class Display(), which hides the base class implementation through the use of the new keyword:

b = new DC();
b.Display(); 

I would like to know what the new keyword is doing internally.

The source of this code included the following explanation:

Since b contains a reference to an object of type DC one would expect the function Display() of class DC to get executed. But that does not happen. Instead what is executed is the Display() of BC class. That’s because the function is invoked based on type of the reference and not to what the reference variable b refers to. Since b is a reference of type BC, the function Display() of class BC will be invoked, no matter whom b refers to.

I am very confused about this particular bit: “because the function is invoked based on type of the reference and not to what the reference variable b refers to”

What does “function is invoked based on type of the reference” mean here

b = new DC();
b.Display();  

What is the type of b here? It was declared as instance name of class BC
but later b becomes an instance of class DC.

  • 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-23T20:33:24+00:00Added an answer on May 23, 2026 at 8:33 pm

    What you are doing is not overriding, but shadowing. The new keyword lets you have a Display method in both the classes BC and DC, but the methods are not related at all, they just have the same name.

    To override the method you would need to use the virtual keyword for the method in the BC class, and the overrides method in the DC class.

    When you are shadowing a method, it’s the type of the reference that decides which method is used:

    BC b1;
    b1 = new BC();
    b1.Display(); // Calls the method in BC
    
    BC b2;
    b2 = new DC();
    b2.Display(); // Calls the method in BC
    
    DC d1;
    d1 = new DC();
    d1.Display(); // Calls the method in DC
    

    Overriding the method looks like this:

    class BC {
    
      public virtual void Display() {
        System.Console.WriteLine("BC::Display");
      }
    
    }
    
    class DC : BC {
    
      override public void Display() {
        System.Console.WriteLine("DC::Display");
      }
    
    }
    

    When overriding a method, the methods are related, and it’s the actual type of the object that decides which method is used, not the type of the reference:

    BC b1;
    b1 = new BC();
    b1.Display(); // Calls the method in BC
    
    BC b2;
    b2 = new DC();
    b2.Display(); // Calls the method in DC
    
    DC d1;
    d1 = new DC();
    d1.Display(); // Calls the method in DC
    

    Another difference between overriding and shadowing, is that when you shadow a method the don’t have to be similar at all, the new keyword just tells the compiler that you want to reuse the identifier for something other than in the base class. When overriding a method, the method signatures have to be the same.

    You can for example shadow a public method that takes a string with something completely different like a private property of the type int:

    public class X {
    
      public void XX(string z) { }
    
    }
    
    public class Y : X {
    
      private new int XX { get; set; }
    
    }
    
    X x = new Y();
    x.XX();
    
    Y y = new Y();
    y.XX = 42;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following: public abstract class Base { // other stuff public static void
Confusing question, I know. Given the following: class Test { public static void GenericFunc<T>(T
Given the following class: class TestClass { public void SetValue(int value) { Value =
Given the following snippet, class Base { public: virtual void eval() const { std::cout<<Base
Given the following: class ParamClass {...}; class MyObject { public: void myMethod(ParamClass const& param)
I have the following piece of code: class Student { public: Student(){} void display()
Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref
I currently have the following code: public class Count { public static void countChar()
Given the following class public class Foo { public int FooId { get; set;
Given the following class definitions: public class BaseClass { public string SomeProp1 { get;

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.