Saturday, March 27, 2010

Object Oriented Programming: Abstraction.


What is Abstraction?

Abstraction is the process of hiding all but the relevant data about an object in order to reduce complexity and increase efficiency. The result of removing the elements not directly related to solving the problem at hand is that you're able to focus specifically on the problem and not be mired in the details of how your class works. The class's interface is the implementation of the abstraction.

This understanding will help you create an interface that gives the user access to the information and methods that they need, yet insulates them from the internal workings of the class. You need to design an interface not only to solve today's problems but also to abstract sufficiently from the class's internals so that private class members can undergo unlimited changes without affecting existing code. The user can then declare/modify attributes, variables and methods within the class without having to worry about clashes with other objects. This basically means that data within a class is only available /modifiable via the class methods.

Always consider the programmer who is going to instantiate or derive from the classes that you create when designing your classes. Designing the abstraction of your classes in a way most useful to the programmers using them is paramount in developing reusable software.

Access Modifiers

Abstraction in C# can be achieved by using Access modifiers when designing your class. Access modifiers are keywords used to specify the declared accessibility of a member (such as a variable, constant or method) or type (which could be a data type such as an integer or a string).

In C# there are 5 different types of Access Modifiers.
Modifier
Description
Public
There are no restrictions on accessing public members.
Private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified
Protected
Access is limited to within the class definition and any class that inherits from the class
Internal
Access is limited exclusively to classes defined within the current project assembly
protected internal
Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.

public: Any code that uses a class can only access the methods that have been marked with the keyword public. The public declaration gives the interface to the external world and defines what the class does, as viewed by the rest of the world.

private: This means that it is not visible outside the class, Marking a field or method as private effectively ensures that that field or method will be part of the internal working of the class, as opposed to the external interface. The advantage of this is that if you decide to change the internal you can just make the change without breaking the code outside the class definition - nothing from outside of this class can access this field.

protected: The protected keyword makes a member accessible within its class and by derived class instances.

internal: The internal keyword makes a member accessible by any code in the same assembly, but not from another assembly. A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of an application’s code.

protectedinternal: The protectedinternal accessibility means protected OR internal, not protected AND internal. In other words, a protectedinternal member is accessible from any class in the same assembly, including any derived class in another assembly.

No comments: