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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:55:20+00:00 2026-05-12T06:55:20+00:00

I’ve found loads of practical examples of this, and understand the practical output when

  • 0

I’ve found loads of practical examples of this, and understand the practical output when overriding or hiding methods, but I’m looking for some under the covers info on why this is and why C# allows it when according to the rules of polymorphism, this shouldn’t be allowed – at least, insofar as my understanding of polymorphism goes (which seems to coincide with the standard definitions found on Wikipedia/Webopedia).

Class Base
{
    public virtual void PrintName()
    {
        Console.WriteLine("BaseClass");
    }
}

Class FirstDerived : Base
{
    public override void PrintName()
    {
        Console.WriteLine("FirstDerived");
    }
}

Class SecondDerived : Base
{
    public new void PrintName()
    {
        Console.WriteLine("SecondDerived");
    }
}

Using the following code:

FirstDerived b = new FirstDerived();
BaseClass a = b;
b.PrintName();
a.PrintName();

I get:

FirstDerived
FirstDerived

Okay, I get that, makes sense.

SecondDerived c = new SecondDerived();
BaseClass a = c;
c.PrintName();
a.PrintName();

I get:

SecondDerived 
BaseClass

Okay, that makes sense, too, instance a can’t see c.PrintName() so it’s using its own method to print its own name, however I can cast my instance to its true type using:

((SecondDerived)a).PrintName();

or

(a as SecondDerived).PrintName();

to get the output I would expect:

SecondDerived

So what is going on under the covers and what does this mean in terms of polymorphism? I’m told that this facility “breaks polymorphism” – and I guess according to the definition, it does. Is that right? Would an “object oriented” langage like C# really allow you to break one of the core principles of OOP?

  • 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-12T06:55:20+00:00Added an answer on May 12, 2026 at 6:55 am

    (This answers the “why is it allowed” which I think is really the central point of your question. How it works in terms of the IL is less interesting to my mind… let me know if you want me to go into that though. Basically it’s just a case of specifying the method to call with a different type token.)

    It allows base classes to evolve without breaking derived classes.

    Suppose Base didn’t originally have the PrintName method. The only way to get at SecondDerived.PrintName would be to have an expression with a static type of SecondDerived, and call it on that. You ship your product, everything is fine.

    Now fast forward to Base introducing a PrintName method. This may or may not have the same semantics of SecondDerived.PrintName – it’s safest to assume that it doesn’t.

    Any callers of Base.PrintName know that they’re calling the new method – they couldn’t have called it before. Any callers which were previously using SecondDerived.PrintName still want to use it though – they don’t want to suddenly end up calling Base.PrintName which could do something entirely different.

    The difficulty is new callers of SecondDerived.PrintName, who may or may not appreciate that this isn’t an override of Base.PrintName. They may be able to notice this from the documentation of course, but it may not be obvious. However, at least we haven’t broken existing code.

    When SecondDerived is recompiled though, the authors will be made aware that there’s now a Base.PrintName class through a warning. They can either stick to their existing non-virtual scheme by adding the new modifier, or make it override the Base.PrintName method. Until they make that decision, they’ll keep getting a warning.

    Versioning and compatibility isn’t usually mentioned in OO theory in my experience, but C# has been designed to try to avoid compatibility nightmares. It doesn’t solve the problem completely, but it does a pretty good job.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.