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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:16:08+00:00 2026-06-05T18:16:08+00:00

Is there some way to define an abstract type as a parameter in an

  • 0

Is there some way to define an abstract type as a parameter in an abstract method, and when that method is implemented in a derived class, you change the type of the method to accept a derived type?

Code:

public abstract class ProductBase
{
}

public class SomeProduct
    : ProductBase
{

}

public abstract class A
{
    protected abstract void addProduct(ProductBase p);
}

// This works
public class B : A
{        
    protected override void addProduct(ProductBase p)
    {
        // Do some work
    }
}

// This is what I'd like to do
public class C : A
{
    // Compiler error here because I have accepted a SomeProduct and not a ProductBase
    protected override void addProduct(SomeProduct p)
    {
        // Do some work on the specialisation - I can use the SomeProduct directly
    }
}

In my head, it makes some kind of sense. An abstract class indicating there is a method which derived classes must implement, but they can change the type of object passed in as a parameter, so long as it is from the same inheritance chain…

What I have ended up doing, is to remove the abstract method AddProduct from the abstract class, and instead just implementing it in the derived class anyway, but there’s then no contract for other classes in the future that they must create their own implementation of AddProduct. And it doesn’t feel right.

I hope this makes sense. Apologies if this is a duplicate question but I couldn’t find anything by searching.

Thanks,
bgs264

  • 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-05T18:16:10+00:00Added an answer on June 5, 2026 at 6:16 pm

    You can make your A class to generic and use the generic argument in your addProduct method:

    public abstract class A<TProduct> where TProduct : ProductBase 
    {
        protected abstract void addProduct(TProduct p);
    }
    
    public class B : A<ProductBase>
    {        
        protected override void addProduct(ProductBase p)
        {
        }
    }
    
    public class C : A<SomeProduct>
    {
        protected override void addProduct(SomeProduct p)
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some way that I can find a user and user's role in
Is there some way to modify qmake project files (*.pro) such that qmake doesn't
Is there some way when sending this message to specify that I rather have
Is there some way I can catch when a method generates a write to
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Is there some way to add to the existing maven configurations in the run
Is there some way to represent this kind of SQL with JPA or Hibernate
Is there some way to write for loop with double init-expression? vector<int> v; //...vector
Is there some way to compile (with the CSC task) a WPF Application with
is there some way to force the gridview's pager to show up, even when

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.