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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:54:33+00:00 2026-06-07T02:54:33+00:00

this is a small code which shows virtual methods. class A { public virtual

  • 0

this is a small code which shows virtual methods.

class A
{
  public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
  public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
  new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
  public override void F() { Console.WriteLine("D.F"); }
}

class Test
{
  static void Main() 
  {
    D d = new D();
    A a = d;
    B b = d;        

    a.F();
    b.F();        
 }
}

This code is printing the below output:

B.F
B.F

I can not understand why a.F() will print B.F ?

I thought it will print D.F because Class B overrides the F() of Class A, then this method is being hidden in Class C using the “new” keyword, then it’s again being overridden in Class D. So finally D.F stays.

but it’s not doing that. Could you please explain why it is printing B.F?

  • 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-07T02:54:35+00:00Added an answer on June 7, 2026 at 2:54 am
    A a = d;
    a.F();
    

    It will find F() as follows.

    1. First in class A
    2. Then in class B
    3. Then in class C
    4. Then in class D

    Now F() will be found in A, and B. Thus B.F() will be invoked. In class C F() is different (as it is new implementation and does not override from A/B). So In 3rd step, c.F() will not be found. In Class D, it overrides new function created by C, thus it will also not be found.

    Due to new keyword, the resulting code is like following (with respect to virtual override)

        class A
        {
            public virtual void F() { Console.WriteLine("A.F"); }
        }
        class B: A
        {
            public override void F() { Console.WriteLine("B.F"); }
        }
        class C: B
        {
            public virtual void F1() { Console.WriteLine("C.F"); }
        }
        class D: C
        {
            public override void F1() { Console.WriteLine("D.F"); }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Work on this small test application to learn threading/locking. I have the following code,
Boost Network I am trying out this code in a small console app on
I've found this small code that sends email to gmail users. I'd like the
This is small code. But I couldn't find whats wrong with it. In my
I have this small piece of code that basically takes a list and runs
Using this code to zip a folder and it works perfect on small files
I use this code , to log $_SERVER['REMOTE_ADDR']; to my small db my issue
I just inherited code which hides/shows rows a UITableView by using the delegate heightForRowAtIndexPath
I just wrote a small program which takes the input from user and shows
I managed to apply this excellent example to my needs: http://code.google.com/p/galleriffic/issues/attachmentText?id=76&aid=-3045121742886940548&name=example-2.html&token=4nlvmgAfVpc5whcXC9UEqHZaSz0%3A1339257750721 With a small

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.