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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:12:17+00:00 2026-05-28T03:12:17+00:00

Let’s suppose that we have 3 classes: Base , Root and Child . public

  • 0

Let’s suppose that we have 3 classes: Base, Root and Child.

public abstract class Base
{
    protected int _Value;

    public double DoSomeWork(int value)
    {
        _Value = value;
        double result = Calculate();

        return result;
    }

    public abstract double Calculate();

    public Child CreateChild(int length)
    {
        return new Child(this);
    }
}

public class Root : Base
{
    public override double Calculate()
    {
        return _Value;
    }
}

public class Child : Base
{
    readonly Base _Container;

    public Child(Base container)
    {
        _Container = container;
    }

    public override double Calculate()
    {
        double result = _Container.Calculate();
        // do some more calculation

        return result;
    }
}

My issue here is that I would like only the DoSomeWork (and CreateChild) be publicly accessible, but in my “architecture” I am forced to make Calculate public also. Or am I?

Any input will be very much appreciated.

Edit:

Calculate cannot be protected because of this line in Child

double result = _Container.Calculate();

which would cause a build error to occur.

  • 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-05-28T03:12:18+00:00Added an answer on May 28, 2026 at 3:12 am

    No, you can make it protected. That way your derived class will still have access to it and be able to override it, but it won’t be allowed publicly.

    Good thinking, this is exactly the way to do it: override as little as possible and hide as much as possible.

    – Edit –

    Since this is probably a simplified example of what you are actually doing, I can only provide a guess to what these methods do, but here it goes.

    I have a feeling that Calculate method might not belong to your Base class. It looks like it provides an auxiliary calculation result used by DoSomeWork. Inheritance is usually used when your base class has some common calculation to “offer” to derived classes, so that you don’t have to repeat yourself.

    For example, let’s say that your DoSomeWork method has some certain functionality that doesn’t change, but requires an “external” calculation to be performed first. If you started by creating a simple separate interface for the external calculation:

    interface ICalculator
    {
        double Calculate();
    }
    

    You could define different implementations of this interface. You can start by creating a simple implementation (similar to your Root functionality):

    class SimpleCalculator : ICalculator
    {
        readonly double _value;
        public SimpleCalculator(double value)
        {
           _value = value;
        }
    
        public double Calculate()
        {
           return _value;
        }
    }
    

    And you could also easily wrap existing implementations inside more complex classes (similar to what CreateChild intends to do):

    // for the rest of the world, this is an ICalculator like any other
    class CalculatorWrapper : ICalculator
    { 
        readonly ICalculator _base;
        public CalculatorWrapper(ICalculator baseCalc)
        {
           _base = baseCalc;
        }
    
        public double Calculate()
        {
           double value = _base.Value; 
           return 2 * value;
        }
    }
    

    And then, your actual class needs to use this functionality to some intended “extra work”:

    interface MyWorker
    {
        double DoSomeWork(int value);
    }
    
    class YourClass
    {
        readonly ICalculator _calc; 
        readonly double _someOtherValue;
    
        public YourClass(ICalculator calc, double someOtherValue)
        {
           _calc = calc;
           _someOtherValue = someOtherValue;
        }
    
        public double DoSomeWork(int value)
        {
           // use whatever you get from your calc
           double externalValue = _calc.Calculate();
    
           // and do some "actual work"
           return _someOtherValue + value + externalValue;
        }
    }
    

    Or, you could pass the “calculator” to DoSomeWork on each call, as a parameter, and change the complex class to something like:

    interface MyWorker
    {
        double DoSomeWork(ICalculator calc, int value);
    }
    
    class YourClass
    {
        public double DoSomeWork(ICalculator calc, int value)
        {
           // use whatever you get from your calc
           double externalValue = calc.Calculate();
    
           // and do some "actual work"
           return _someOtherValue + value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I have the following entity: public class Store { public List<Product> Products
Let's suppose that we have multi-site CMS and every website in this CMS having
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's imagine I have a Java class of the type: public class MyClass {
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>

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.