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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:46:51+00:00 2026-06-04T02:46:51+00:00

( I tagged this as both C# and Java, since it’s the same question

  • 0

(I tagged this as both C# and Java, since it’s the same question in both languages.)

Say I have these classes

interface IKernel
{
    // Useful members, e.g. AvailableMemory, TotalMemory, etc.
}

class Kernel : IKernel
{
    private /*readonly*/ FileManager fileManager;  // Every kernel has 1 file manager
    public Kernel() { this.fileManager = new FileManager(this); /* etc. */ }

    // implements the interface; members are overridable
}

class FileManager
{
    private /*readonly*/ IKernel kernel;  // Every file manager belongs to 1 kernel
    public FileManager(IKernel kernel) { this.kernel = kernel; /* etc. */ }
}

The problem with this design is that as soon as FileManager tries to do anything inside its constructor with kernel (which it might reasonably need to), it will be calling a virtual method on a potential subclass instance whose constructor is not yet called.

This problem doesn’t occur in languages where you can define true constructors (rather than initializers, like C#/Java), since then the subclasses don’t even exist before their constructors are called… but here, this problem happens.

So what is the best/proper design/practice, to ensure this doesn’t happen?

Edit:

I’m not necessarily saying I need circular references, but the fact is that both Kernel and FileManager depend on each other. If you have a suggestion on how to alleviate this problem without using circular references, then that’s great too!

  • 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-04T02:46:52+00:00Added an answer on June 4, 2026 at 2:46 am

    Personally, I don’t like circular references. But if you decide to leave them, you may add some laziness:

    interface IKernel
    {
        // Useful members, e.g. AvailableMemory, TotalMemory, etc.
    }
    
    class Kernel : IKernel
    {
        private readonly Lazy<FileManager> fileManager;  // Every kernel has 1 file manager
        public Kernel() { this.fileManager = new Lazy<FileManager>(() => new FileManager(this)); /* etc. */ }
    
        // implements the interface; members are overridable
    }
    
    class FileManager
    {
        private /*readonly*/ IKernel kernel;  // Every file manager belongs to 1 kernel
        public FileManager(IKernel kernel) { this.kernel = kernel; /* etc. */ }
    }  
    

    Laziness here lets ensure, that IKernel implementation will be initialized completely, when FileManager instance will be queried.

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

Sidebar

Related Questions

I tagged this question both Java and Scala because while I mainly develop on
This post is incorrectly tagged 'send' since I cannot create new tags. I have
I have this piece of php - mysql code: When a user tagged in
This is in C, but I tagged it C++ incase it's the same. This
I've already found this question on SO (which is exactly the same problem I'm
Late Edit I tagged this as a C# question as well as C++ because
Lets say you have this: P1 = (x=2, y=50) P2 = (x=9, y=40) P3
this is my first question on StackOverflow, but I think that we'll both come
EDIT: I've tagged this C in a hope to get more response. It's more
First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow

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.