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

The Archive Base Latest Questions

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

I currently have an inheritance structure where the return type of a method is

  • 0

I currently have an inheritance structure where the return type of a method is being left as an open generic so that each class can return an object from the corresponding level of another structure. For example, lets say we had a VehicleFactory where TVehicle:Vehicle that could ProduceVehicle TVehicle. I also have CarFactory:VehicleFactory where TVehicle:Car.

Car inherits from Vehicle, so all this is valid and allows me to know that my CarFactory must produce Cars and my Vehicle factory may produce any kind of vehicle. The problem I have run in to is that I need a way to instantiate a VehicleFactory as a CarFactory when it is being run by Ford, but as a BoatFactory when being run by Wave Runner.

I thought I could do this by creating an interface that matched the functionality of VehicleFactory and writing a MakeFactory method that returns an IVehicleFactory (which would return vehicles non-genericly). Since CarFactory returns cars, which are vehicles, it fulfills the interface and all should be right in the world. The unexpected problem is that VehicleFactory fails to meet the interface that is closed as TVehicle being Vehicle, despite the fact that TVehicle must be a Vehicle.

Does anyone know why this is or if there is another way to work around this limitation? If there isn’t a way around this limitation directly, are there any alternative methods for having a shared set of functionality ensure that it is always instantiated as one of two or more sets of more specific classes. (Where Vehicle is the shared layer and Car and Boat are the context specific layers.)

class Vehicle
{
}

class Car : Vehicle
{
}

interface IVehicleFactory
{
     Vehicle ProduceVehicle();
}

class VehicleFactory<TVehicle> : IVehicleFactory
    where TVehicle:Vehicle
{
    public virtual TVehicle ProduceVehicle()
    {
    }
}

class CarFactory<TVehicle> : VehicleFactory<TVehicle>
    where TVehicle : Car
{
    public override TVehicle ProduceVehicle()
    {
    }
}

And the code using it

static IVehicleFactory CreateVehicleFactory()
{
    if(somecondition)
    {
         Return new CarFactory<Car>();
    }
    else
    {
         Return new BoatFactory<Boat>();
    }
}

Adding some more detail to clarify the problem.

The use of the term factory is not intended to imply a factory pattern. It is actually a repository that is retrieving a “vehicle.” The type of vehicle is an application specific version of a library with a common shared base code. The repositories for each application Car and Boat may have different logic for the same retrieval and may be dependent on fields in the Car or Boat variant of their object. I need a way to build a factory which can look at the context of the application I am in and return the appropriate repository (BoatFactory or CarFactory), such that the shared code at the Vehicle level will properly use any application specific overridden functionality.

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

    Well, while I don’t have a solution to how to make it work yet, (at least not in full), I think I now understand the problem. One of my initial assumptions appears to have been incorrect. I was under the impression that a less specific return type on an interface could be fulfilled by a more specific type. ie, an interface defining SomeBaseClass as a return type for a method can not be implemented by a class that implements the method returning SomeChildClass where SomeChildClass inherits from SomeBaseClass.

    The use of covariant generics (via the out keyword) does provide some manner of ability to circumvent this by allowing the interface to be generically defined, however for my purposes, this falls apart when I hit results that need to be IList based (which requires an invariant type parameter).

    Ultimately, the best it appears that can be done is that I can make it so that CarFactory only understands that it is working with a vehicle (by removing the additional where condition on the TVehicle generic parameter), requiring a cast within CarFactory, however when ever CarCompany instantiates CarFactory, it can still make it as CarFactory and the client code itself will not have to worry about a cast.

    It’s not ideal, but I think it may be the best possible due to C#’s lack of covariant return types.

    interface IVehicleFactory<TVehicle> where TVehicle:Vehicle
    {
        TVehicle SomeMethod();
    }
    
    VehicleFactory<TVehicle> : IVehilceFactory<TVehicle> where TVehicle:Vehicle
    {
        TVehicle SomeMethod()
        {
        }
    }
    
    CarFactory<TVehicle> : IVehicleFactory<TVehicle> where TVehicle:Vehicle
    {
        TVehicle SomeMethod()
        {
           ~~Always returns car and uses cast from Vehicle when necessary.
        }
    }
    
    class VehicleFactory<TVehicle>
    {
       static IVehicleFactory<TVehicle> CreateContextSpecificFactory()
       {
           if(someCondition)
           {
                return new CarFactory<TVehicle>;
           }
       }
    }
    
    class CarCompany
    {
        CarFactory<Car> carFactory = new CarFactory<Car>;
        Car car = carFactory.SomeMethod();
    }
    
    class VehicleCompany
    {
        VehicleFactory<Vehicle> vehicleFactory = VehicleFactory<Vehicle>.CreateContextSpecificFactory();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain model that is currently heavily relying on inheritance, and due
Currently I have a class called user that I want to create with different
I currently have an array of type Class which holds only sub-classes of type
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I currently have a simple iPhone app that loads a custom subclass of UIView.
I am new to JPA/Hibernate. Currently using EJB3, Hibernate/JPA. I have an inheritacnce structure
I currently have a socket application that I use to send string back and

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.