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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:58:27+00:00 2026-06-13T22:58:27+00:00

Ok lets say I have two classes with the same method(sampleMethod) which is overridden

  • 0

Ok lets say I have two classes with the same method(sampleMethod) which is overridden by the child: ObjectParent and ObjectChild

ObjectChild ExampleVariable = new ObjectChild();
(ExampleVariable as ObjectParent).sampleMethod();

Will this call the sampleMethod from ObjectChild or from ObjectParent??

I think it will call the sampleMethod from ObjectChild, but I want to make sure before I throw down a bunch of code based on that assumption.

  • 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-13T22:58:28+00:00Added an answer on June 13, 2026 at 10:58 pm

    Will this call the sampleMethod from ObjectChild or from ObjectParent?

    Assuming it’s really a virtual method which is properly overridden (rather than just being hidden), it will call the ObjectChild implementation. That’s the whole point of virtual methods – that you don’t need to know the execution-time type at compile-time. For example, I can write a method using Stream.Read (e.g. by taking the Stream as a parameter) without knowing which implementation of Stream it will end up using. At execution time that code could end up reading from the network, from memory, from disk… my method’s code neither knows nor case.

    Short but complete program demonstrating this:

    using System;
    
    class ObjectParent
    {
        public virtual void Foo()
        {
            Console.WriteLine("ObjectParent.Foo");
        }
    }
    
    class ObjectChild : ObjectParent
    {
        public override void Foo()
        {
            Console.WriteLine("ObjectChild.Foo");
        }
    }
    
    class Test
    {
        static void Main()
        {
            // Simpler code to demonstrate the point
            ObjectParent parent = new ObjectChild();
            parent.Foo(); // Prints ObjectChild.Foo
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have two classes A and B, which have theirs internal state
Let's say I have two classes, A and B, where B is a child
So, lets say I have two nearly identical classes in C# and Ruby: C#
Lets say you have two classes, Boss and Employee. There is a bidirectional many-to-many
Let's say I have two classes. Both have the same member fields (including the
Lets say I have a two classes like this: public class LocalResources { public
Let's say I have two classes, Parent and Child. These two are related via
I have two objects that are derived from same the base class. Lets say
Let's say we have these two classes: public class Base { public static int
Let's say I have a base class 'A' and two classes 'B' and 'C',

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.