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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:42:33+00:00 2026-05-20T11:42:33+00:00

I have a class B that inherits from class A, which in turn inherits

  • 0

I have a class B that inherits from class A, which in turn inherits from interface I. This interface exposes a method M which, of course, is implemented by A, but I would like to override it in B. Moreover, I would like to call A.M from B.M. How do I do that?


EDIT: The answers made me feel kind of stupid, especially since I know what virtual means and, in fact, I have tried it:

class A
{
    virtual void I.M()           // fails

I never even considered not implementing the interface explicitly.

Thank you all.

  • 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-20T11:42:34+00:00Added an answer on May 20, 2026 at 11:42 am

    Well, either you need to make the method virtual in A, or you need to reimplement the interface in B, which gets messy. Here’s the simpler version:

    using System;
    
    public interface IFoo
    {
        void M();
    }
    
    public class A : IFoo
    {
        public virtual void M()
        {
            Console.WriteLine("A.M");
        }
    }
    
    public class B : A
    {
        public override void M()
        {
            base.M();
            Console.WriteLine("B.M");
        }
    }
    
    class Test
    {
        static void Main()
        {
            IFoo foo = new B();
            foo.M();
        }
    }
    

    … and here’s the version which reimplements IFoo, hiding A.M() instead of overriding it:

    using System;
    
    public interface IFoo
    {
        void M();
    }
    
    public class A : IFoo
    {
        public void M()
        {
            Console.WriteLine("A.M");
        }
    }
    
    public class B : A, IFoo
    {
        public new void M()
        {
            base.M();
            Console.WriteLine("B.M");
        }
    }
    
    class Test
    {
        static void Main()
        {
            IFoo foo = new B();
            foo.M();
        }
    }
    

    Note that if you then had:

    A a = (A) foo;
    a.M();
    

    it would only call A.M(), not B.M().

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

Sidebar

Related Questions

I have a class that inherits from a base class. This base class exposes
I have a class that inherits from a base class (this contains a lot
I have a custom control that inherits from .NET's CompositeControl class. This control overrides
I have a class that inherits from DrawingVisual. It declares a DependencyProperty which, when
I have a class that inherits from System.Web.Mvc.RedirectResult which overrides ExecuteResult . How do
Alright lets say I have a cPlayer class that inherits from cOrganism , which
I have a class that inherits from Hash . When this class itself gets
I have a web form page Default.aspx, which inherits from a BasePage class that
I have a class that inherits from a generic dictionary as follows: Class myClass
I have a class that inherits from Page, called APage. public abstract class APage:

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.