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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:49:21+00:00 2026-05-16T16:49:21+00:00

1) One reason to use encapsulation is that by hidding internal implementation details we

  • 0

1) One reason to use encapsulation is that by hidding internal implementation details we are able to modify this details without breaking the existing code – this makes perfect sense

a) But I don’t understand the argument that encapsulation should be used in order to prevent users from setting the internal data of an object into an invalid or incosistent state.

If I write a class and sell it to other programmers ( who implement this class into their own applications ), then shouldn’t they be responsible to access this object in a correct way? After all, if they don’t use the object correctly then it will only be their applications that will suffer because of it.

An analogy ( a bad one ) of me having to implement encapsulation for the sake of other programmers not putting internal data ( either accidentally or on purpose )into incosistent state, would be selling water proof TVs just in case buyers decide to swim with their TV sets.

2) From

http://blogs.msdn.com/b/ericlippert/archive/2007/10/19/covariance-and-contravariance-in-c-part-three-member-group-conversion-variance.aspx

void Foo(Giraffe g) {}
void Bar(Animal a) {}

Action<Mammal> action1 = Foo; // illegal 
Action<Mammal> action2 = Bar; // legal

Why is the first assignment illegal?
Because the caller of action1 can pass
a Tiger, but Foo cannot take a Tiger,
only a Giraffe! The second assignment
is legal because Bar can take any
Animal.«

I understand author’s reasoning on why Action<Mammal> action1 shouldn’t be able to accept Foo(). But on the other hand, shouldn’t it be the programmer’s responsibility to not pass into delegate any arguments that registered methods can’t handle?

Thus, if some programer registers method Foo, then they will also know :

• not to call action1 delegate with anything else than with Giraffe argument

• Not to register methods with different signature than Foo ( except if method defines as its parameter a type from which Mammal derives )

thanx

  • 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-16T16:49:22+00:00Added an answer on May 16, 2026 at 4:49 pm

    One of the advantages of good encapsulation is to “force” the user of the class to use it in the proper way, and not in any other possible way. This way, the user is less frustrated about the code he has to (re)use. The productivity increases (less time spent figuring out why the code breaks, especially if the source code is not available), etc.

    If I have to buy a library from someone, and I have to be very careful on how I have to use it at each step, I would consider that I wasted money.

    (I excluded the library design topics, stability, maintainability and extensibility. I am mortal, and the time to cover these aspects is limited :)…)

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

Sidebar

Related Questions

No related questions found

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.