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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:43:08+00:00 2026-05-18T01:43:08+00:00

I have a base class which contains an abstract/MustInherit method. I want this method

  • 0

I have a base class which contains an abstract/MustInherit method. I want this method to return an instance of a class which implements a given interface.

When I come to implement the method in the derived class, I get a compiler error because the return types do not match (ie. in the abstract class, the return type is that of the interface, whereas in the derived class the return type is the class which implements the interface).

Can anyone clarify the underlying problem for me? I can’t quite get my head around what’s wrong. Especially given that the specified return type of the abstract method is an interface, which can’t be instantiated, so surely the method implementation must return an instance of class which implements the interface? Maybe I just can’t see the wood for the trees.

I can code around the issue by making the base class generic (and I can specify that T must implement my interface), then the abstract method would return an instance of T. I’m not sure about that solution though. It seems a bit of a hack, I don’t get why I can’t do it with straight inheritance.

  • 1 1 Answer
  • 1 View
  • 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-18T01:43:08+00:00Added an answer on May 18, 2026 at 1:43 am

    Here is one way you could accomplish what you’re after:

    public interface ISomething {}
    public class Something : ISomething {}
    
    public abstract class Base
    {
        public ISomething Method()
        {
            return DoMethod();
        }
    
        protected abstract ISomething DoMethod();
    }
    
    public class Derived : Base
    {
        public new Something Method()
        {
            return (Something) DoMethod();
        }
    
        protected override ISomething DoMethod()
        {
            return null; //derived logic here
        }
    }
    

    The issue is that overridden methods are constrained to have the exact same signature as their base. Suppose you were to create a value type that implements ISomething and then specialized the override in Derived to return the value type. Now the overridden method is returning a value type that implements the interface, but, calling the method from the Base, you would expect to get a boxed value type of the form of the interface. If the Derived call returned an unboxed value when calling through the Base, the calling code would try to access it as if it were already boxed, causing a failure.

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

Sidebar

Related Questions

I have an abstract base class which contains a private nested implementation. visual c++
I want to have a base class which dictates the alignment of the objects
I have an abstract base class and two derived classes. The base class contains
I have a base class which contains following properties: public virtual Vector3b SIZE {
I have a base class Node which contains a list of child nodes. Node
The problem: I have a class which contains a template method execute which calls
Ok, so I have a base class which declares the event StatusTextChanged . My
I have a Exception base class which defines a stream function: class Exception {
I have a class which is derived from a base class, and have many
I have written a base class from which I wish to derive several child

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.