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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:58:09+00:00 2026-05-30T12:58:09+00:00

How can inheritance break encapsulation if we only use protected and private access modifiers

  • 0

How can inheritance break encapsulation if we only use protected and private access modifiers in the superclass?

It is sometimes claimed inheritance exposes the superclass implementation. Surely this is not true if the member variables are declared as private? If this refers to changes being made in the superclass will break the subclass, couldnt this affect non-inheritance too?

I also fail to see how composition is any different, with regards to exposing the underlying implementation? We can only use public functions using the composition approach- surely this exposes more?

  • 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-30T12:58:10+00:00Added an answer on May 30, 2026 at 12:58 pm

    A deriving class can have access to parts of the implementation of the base class. Eg. if you used protected for any or all members in the base class then the base class would it self be unable to enforce it’s own invariants. This could break the functionality of the base class. Changing the name of a field or removing or adding one might cause issues for the deriving class unless they are private, so again exposing the implementation might break the otherwise correct code.

    The template pattern is a deliberate way of exposing part of the implementation of the base class, that it’s deliberate does not mean it’s not exposing (parts of) the implementation. Using composition (strategy) to get the same rsult (usually related to open/closed principle) has a looser coupling.
    E.g. if you change the signature of one of the virtual methods (e.g. the name) or add a new (abstract) method used in the template pattern that change cascade to all deriving classes. In the case of composition you can change the name of all the methods in the class that does the composing without changing any of the classes you compose from. You can also add another step to the algorithm with out changing the other classes (as long as one of the parts you compose from already supports this functionality)

    A different way of looking at why virtual methods can break encapsulation:

    Take this class

    class ArrayPrinter{
       private bool CanContinue<T>(T[] array, int newIndex){
          return newIndex<array.Length;
       }
    
       public void Print<T>(T[] array){
          for(var i = 0;CanContinue(array,i);i++){
             Console.WriteLine(string.Format("{0}",array[i]);
          }
       }
    }
    

    In the above example we can be sure that the implementation of Print will not result in an index out of bounds (thread safety aside) and that all elements of the array will be printed (unless Console.WriteLine or string.Fromat fails). If however we change the signature of CanContinue to the below

       protected virtual bool CanContinue<T>(t[] array, int newIndex)
    

    There’s no longer any gaurantee that ArrayPrinter will work correctly. We’ve made it possible to change the implementation of Print from the outside of the class and hence have broken encapsulation. SOme might argue you only break encapsulation if you expose your fields. I don’t share that oppinion the below code suffers the exact same problem as the above, though it’s exposing a Field (through a property, just to show that hidding you’re fields behind a property doesn’t ensure that you haven’t broken encapsulation)

    class ArrayPrinter<T>{
       private Func<T[],int,bool> _canContinue;
       protected Func<T[],int,bool> CanContinue{
               get{
                   return _canContinue ?? canContinue;
               }
               set{
                    _canContinue = value;
               }
       }
       private bool canContinue(T[] array, int newIndex){
          return newIndex<array.Length;
       }
    
       public void Print<T>(T[] array){
          for(var i = 0;CanContinue(array,i);i++){
             Console.WriteLine(string.Format("{0}",array[i]);
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Private/protected inheritance Can you give me example for private inheritance in C++
Whats the other option we can use for multiple inheritance other than implementing interface
i know how to do inheritance in javascript but i can only inherit a
I know that you can't use inheritance with enums, so in my base class
I read (Scott Myers) that inheritance breaks encapsulation. When data/internal methods are private (not
When creating a child process in C++ using Windows API, one can allow inheritance
Now while I know that you can not perform inheritance like you would in
Can some one explain to me the difference between categories and inheritance in Objective
I can see people asking all the time whether multiple inheritance should be included
I know with OO Perl I can have objects and inheritance, but are interfaces

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.