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

  • Home
  • SEARCH
  • 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 7034317
In Process

The Archive Base Latest Questions

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

Could someone please explain what the difference is between these two examples? Class A

  • 0

Could someone please explain what the difference is between these two examples?

Class A

protected virtual string GetData()

Class B

private override string GetData()

And the following:

Class A

protected string GetData()

Class B

private string GetData()

Assuming that ‘Class B’ inherits from ‘Class A’.

I always assumed that you need to use virtual in the superclass and override in the subclass if you want overriding of a method, however I tried removing the keywords and the program compiled fine. What exactly is the difference, if any?

  • 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-28T01:10:16+00:00Added an answer on May 28, 2026 at 1:10 am

    The second example that you showed hides the GetData of the parent, it doesn’t override it.

    Example:

    private class Base
    {
        public virtual void Test()
        {
            Console.WriteLine("Base");
        }
    
        public void Test2()
        {
            Console.WriteLine("Base");
        }
    }
    
    private class Derived : Base
    {
        public override void Test()
        {
            Console.WriteLine("Derived");
        }
    
        public void Test2()
        {
            Console.WriteLine("Derived");
        }
    }
    
    static void Main()
    {
        Base b = new Base();
        Derived d = new Derived();
        Base dInB = new Derived();
    
        b.Test();
        d.Test();
        dInB.Test();
    
        b.Test2();
        d.Test2();
        dInB.Test2();
    
        Console.ReadKey(true);
    }
    

    It outputs:

    Base    // Base.Test()
    Derived // Derived.Test()
    Derived // Derived.Test()
    Base    // Base.Test2()
    Derived // Derived.Test2()
    Base    // You think you're calling Derived.Test2(), but you actually call Base.Test2()
    

    Actually this sample is invalid, because it should use the new keyword in public new void Test2() in the Derived class.

    It works just like operator overloading. It doesn’t actually override anything. When you have the exact type Derived it calls the new method.

    You have to be really careful with hiding members, it is nothing like overriding (classes) or implementing (interfaces) at all. Only when you have the exact type it’ll call a new method, otherwise it’ll still call the base type’s method!

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

Sidebar

Related Questions

Please could someone explain me what's the difference between public and published class members
Could someone please explain the Objective-C difference between myString and anotherString in the following
Could someone please explain the significant difference in speed between a firefox updatepanel async
Could someone please explain to me the difference between the combo box that's available
Could someone please explain what's the difference between inheriting from ISerializable interface and declaring
I am learning jQuery. Could someone please explain what the difference between the window.onload
Could someone please explain a bit, what difference between them? For example I could
Could someone please explain to me what the main differences are between JPA and
Could someone please explain the difference? From what I understand, I could expose my
Could someone please explain the difference in how the 2 snippets of code are

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.