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.

Thursday, March 11, 2010

Object Oriented Programming: Encapsulation



What is Encapsulation (or information hiding)?

Encapsulation (as in enclosed in a capsule), sometimes called information hiding, is the ability to hide the internals of an object from its users and to provide an interface to only those members that you want the client to be able to directly manipulate. 

Encapsulation provides the boundary between a class's external interface, its public members visible to the class's users and its internal implementation details. The result is that each object exposes to any class a certain interface (those members accessible to that class). The interface must encapsulate the implementation - hide it from other parts of a program and protect an implementation from unintended actions and inadvertent access, exposing only the members of a class that will remain static, or unchanged, while hiding the more dynamic and volatile class internals. 

The advantage of encapsulation means once the interface to the object is designed you don't have to be concerned as others work on it, fix bugs and find better ways to implement it. You'll get the benefit of these improvements but none of them will affect what you do in your program. Because you're depending solely on the interface nothing they do can break your code. Your program is insulated from the object's implementation. The implementation is insulated from anything that you or other users of the object might do.

Wednesday, March 03, 2010

Object Oriented Programming: The Class


What is a Class?
The difference between a Class and an object is a source of a lot of confusion for programmers new to the terminology of object-oriented programming. The basic building blocks of object-oriented programming are the class and the object.

In object-oriented programming, a Class is a construct which is defined by a programmer in code and is used as a template (or blueprint) to create objects of that class. This template describes the state and behaviour that the objects of the Class all share.

A class encapsulates the state and behaviour of the concept it represents.
·         It encapsulates state through data placeholders called attributes (or member variables);
·         It encapsulates behaviour through reusable sections of code called methods.

A class is used to create new instances (Objects) by instantiating the class. An Object doesn't exist until an instance of the class has been created. The class is just a definition.