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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:45:23+00:00 2026-05-10T14:45:23+00:00

Suppose I have BaseClass with public methods A and B, and I create DerivedClass

  • 0

Suppose I have BaseClass with public methods A and B, and I create DerivedClass through inheritance.

e.g.

public DerivedClass : BaseClass {} 

Now I want to develop a method C in DerivedClass that uses A and B. Is there a way I can override methods A and B to be private in DerivedClass so that only method C is exposed to someone who wants to use my DerivedClass?

  • 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. 2026-05-10T14:45:24+00:00Added an answer on May 10, 2026 at 2:45 pm

    It’s not possible, why?

    In C#, it is forced upon you that if you inherit public methods, you must make them public. Otherwise they expect you not to derive from the class in the first place.

    Instead of using the is-a relationship, you would have to use the has-a relationship.

    The language designers don’t allow this on purpose so that you use inheritance more properly.

    For example one might accidentally confuse a class Car to derive from a class Engine to get it’s functionality. But an Engine is functionality that is used by the car. So you would want to use the has-a relationship. The user of the Car does not want to have access to the interface of the Engine. And the Car itself should not confuse the Engine’s methods with it’s own. Nor Car’s future derivations.

    So they don’t allow it to protect you from bad inheritance hierarchies.

    What should you do instead?

    Instead you should implement interfaces. This leaves you free to have functionality using the has-a relationship.

    Other languages:

    In C++ you simply specify a modifier before the base class of private, public or protected. This makes all members of the base that were public to that specified access level. It seems silly to me that you can’t do the same in C#.

    The restructured code:

    interface I {     void C(); }  class BaseClass {     public void A() { MessageBox.Show('A'); }     public void B() { MessageBox.Show('B'); } }  class Derived : I {     public void C()     {         b.A();         b.B();     }      private BaseClass b; } 

    I understand the names of the above classes are a little moot 🙂

    Other suggestions:

    Others have suggested to make A() and B() public and throw exceptions. But this doesn’t make a friendly class for people to use and it doesn’t really make sense.

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

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In one of my applications I often want the first… May 12, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer Value types, such as Int32, are structs. From the VS… May 12, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer The redirect happens because the user is not authorized to… May 12, 2026 at 11:40 pm

Related Questions

Suppose I have this model: public class ViewModel { [Required] public string UserInput {
For a little background information, I've got an application that's running in a loop,
I have several classes that conceptually belong to one tier. They have no common
Suppose I have several small test cases. Easy enough to setup and easy enough

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.