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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:27:12+00:00 2026-06-17T16:27:12+00:00

Below code: public class Program { static void Main(string[] args) { father f =

  • 0

Below code:

 public class Program
    {
        static void Main(string[] args)
        {
            father f = new son(); Console.WriteLine(f.GetType());
            f.show();
         }
    }

    public class father
    {
      virtual public void show()
        {
            Console.WriteLine("father");
        }
    }

    public class son : father
    {
        public override void  show()
        {
            Console.WriteLine("son");
        }
    }

The result is ‘son’.
If I modify the ‘public override void show()‘ to ‘public new void show()‘,the result is ‘father’.

So I conclude below ‘rules’:

  • Use ‘override’, which function will be called is determined in run
    time. The program will choose the right function according to the
    real type of current object.(As above, the f’s runtime type is son,
    so it calls the son’s show.)
  • Use ‘new’ modifier, which function will be called is determined when
    it is compiled.The program will choose the object’s declared type to
    call its function.(As above, the f’s declared type is father ,so
    using ‘new’ modifier make the output to show ‘father’.

All above are what I understand about polymorphism.Any misunderstanding and wrong?

  • 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-06-17T16:27:13+00:00Added an answer on June 17, 2026 at 4:27 pm

    Use ‘new’ modifier, which function will be called is determined when it is compiled.The program will choose the object’s declared type to call its function.(As above, the f’s declared type is father ,so using ‘new’ modifier make the output to show ‘father’.

    Not really. The decision is still made at execution time, but the new method does not override the virtual method in the base class. This is most easily shown by extending your example somewhat:

    using System;
    
    class Base
    {
        public virtual void Foo()
        {
            Console.WriteLine("Base.Foo");
        }
    }
    
    class Derived : Base
    {
        public override void Foo()
        {
            Console.WriteLine("Derived.Foo");
        }
    }
    
    class MoreDerived : Derived
    {
        public new void Foo()
        {
            Console.WriteLine("MoreDerived.Foo");
        }
    }
    
    class Test
    {
        static void Main()
        {
            Base x = new MoreDerived();
            x.Foo(); // Prints Derived.Foo
        }
    }
    

    Here, at compile time the decision is made to call the most overridden implementation of Base.Foo – if there were multiple Foo signatures, the decision about which signature to use would be taken, for example. Which implementation will be “the most overridden” is unknown at this point, of course.

    At execution time, the CLR will find that most overridden implementation based on the actual type of the target object – which is MoreDerived. But MoreDerived.Foo doesn’t override Base.Foo… whereas Derived.Foo does, so the implementation in Derived is the one which is actually executed.

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

Sidebar

Related Questions

Below is my code. class Program { static void Main(string[] args) { Console.WriteLine(Main thread
please look at the code below. class Program { static void Main(string[] args) {
The below code in Java throws Null pointer exception. public class New{ int i;
I setup a test console application using the below code: using System; class Program
I've a code below: class Person { public string FirstName { get; set; }
I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
I have the code below : public class Anything { public int Data {
I am working with NHibernate and I get this code below: public class User
I having below code for different artifacts, Entity public class ChooseFirst { public int
I have a service with below code TaskService.svc [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class TodoService

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.