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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:16:07+00:00 2026-05-10T18:16:07+00:00

Given the following inheritance tree, what would be the best way of implementing it

  • 0

Given the following inheritance tree, what would be the best way of implementing it in a way that works?

abstract class Foo<T> : IEnumerable<T> {     public abstract Bar CreateBar(); }  class Bar<T> : Foo<T> {     // Bar's provide a proxy interface to Foo's and limit access nicely.     // The general public shouldn't be making these though, they have access     // via CreateBar()     protected Bar(Foo base)     {         // snip...     } }  class Baz<T> : Foo<T> {     public Bar CreateBar()     {         return new Bar(this);     } } 

This fails with: 'Bar.Bar()' is inaccessible due to its protection level.

I don’t want the constructor being public, only classes that inherit from Foo should be able to create Bars. Bar is a specialised Foo, and any type of Foo should be able to create one. Public internal is an ‘option’ here, as the majority of the predefined extensions to Foo will be internal to the DLL, but I consider this a sloppy answer, since anyone who comes along later who wants to create their own type of Foo or Baz (which is likely to happen) will be stuck with a default CreateBar() implementation, which may or may not meet their needs.

Perhaps there is a way of refactoring this to make it work nicely? I’m banging my head on the wall trying to design this so it’ll work though.

Edit (More info):

Slightly more concrete: Foo is implementing IEnumerable and long story short, Bar is providing the same interface, but to a limited subset of that enumerable object. All Foo’s should be able to create subsets of themselves (ie. Bar) and return it. But I don’t want to have everyone who ever wants to implement a Foo to have to worry about this, because Bar will do the proxying and worry about limiting the range, etc.

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

    Okay, new answer:

    1. Split Bar into an interface and a concrete class.
    2. Express the public abstract method in terms of IBar.
    3. Make Bar a private nested class in Foo, implementing IBar. Give it an internal constructor which you can call from Foo.
    4. Write a protected method in Foo which creates an instance of Bar from itself. Classes deriving from Foo can use this to implement the abstract method if just proxying is good enough, and classes with more complicated needs can just implement IBar directly. You could even change the abstract method to a virtual one, and create a new Bar from ‘this’ by default.

    EDIT: One variant on this would be to make Bar a protected nested class within Foo, with a public constructor. That way any derived class would be able to instantiate it for themselves, but no unrelated class would be able to ‘see’ it at all. You’d still need to separate the interface from the implementation (so that the interface can be public) but I think that’s a good thing anyway.

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

Sidebar

Related Questions

Given the following inheritance tree: I have a public class BaseForm : Form This
Given the following situation: @Entity @Inheritance(strategy = InheritanceType.JOINED) @Table(name=TOP_TABLE) public class BaseClass implements Serializable
Given the following example classes in my.package ... public class Foo { public void
Given the following classes: class Foo { struct BarBC { protected: BarBC(uint32_t aKey) :
Given the following code: Class User{ Task m_Task; public function getTask(Do work) { return
assume the following class is given: class Base{ public: Base() {} Base( const Base&
Given the following code: final class retVal { int photo_id; } Gson gson =
Given the following models: team.rb class Team < ActiveRecord::Base has_many :events, :dependent => :destroy
Given this: abstract class ViewPresenterPair { type V <: View type P <: Presenter
I have been reading documentation describing class inheritance, abstract base classes and even python

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.