Understanding Encapsulation

Encapsulation

Is an information-hiding mechanism that makes code easy to maintain and to understand. 

Encapsulation is a mechanism to hide a class or members of a class in order to hide design decisions that are likely to change.

Encapsulation gives the class designer the flexibility to change some section of the code without changing all of the other code that makes use of that code. 

Properties are a great way to hide data fields.

Understanding Access Modifiers

Access modifiers control where a type or type member can be used.

All types and type members have an access level that specifies where that class or its members can be used in your code.


Public

Access is not restricted.

Private

Access is restricted to the containing class.

Protected

Access is restricted to the containing class and to and class that derived directly or indirectly from the containing class.

Internal

Access is restricted to the code assembly.

Protected internal

Access is restricted to any code in the same assembly, and only derived classes in another assembly.

Namespace

Are not allowed to have access modifiers there public by default.

 

Top-Level Class

Can be only public or internal, the default access modifiers for a class is internal.


 You are developing code for a method that calculates the discount for the items sold. You name the method CalculateDiscount. The method defines a variable, percentValue of the type double. You need to make sure that percent value is accessible only within the CalculateDiscount method. What access modifier should you use when defining the percentValue variable?
a) private
b) protected
c) internal

d) public

Answer: a
Difficulty: Medium
Section Reference: Understanding Access Modifiers

The private modifier restricts the access to the class in which the member was defined. The protected modifier restricts the access to the containing class and to any class derived directly or indirectly from the containing class. The internal modifier restricts the access to the code in the same assembly. The public modifier does not restrict access.

You are a C# developer who is developing a Windows application. You develop a new class that must be accessible to all the code packaged in the same assembly. Even the classes that are in the same assembly but do not directly or indirectly inherit from this class must be able to access the code. Any code outside the assembly should not be able to access the new class. Which access modifier should you use to declare the new class?
a) public
b) protected
c) private

d) internal

Answer: d
Difficulty: Medium
Section Reference: Understanding Access Modifiers

For the private access modifier, access is restricted only to the containing class. For the public access modifier, access is not restricted. For the protected access modifier, access is restricted only to the derived classes. For the internal access modifier, access is restricted only to the code in the same assembly.


Comments

Popular posts from this blog

Understanding Polymorphism

Understanding Static Members