Monday, March 16, 2009

Object Oriented Principles (Part 1)

Object:

In the reality every thing is an object. Your pen, PC, cup, car etc are all objects.

Any thing that has physical existence is called an object. Even though you and me are also objects.

Every object has two distinct aspects:

1) Behavior

2) Characteristics

For an easy example, car is an object, it has methods to change gear, to accelerate, to break etc, these methods are simply called behavior of the car and it has properties like color, size, weight, model etc are all characteristics of the car.

Thus you saw that every object has properties which can be accessed by its methods.

In programming concepts every object has variables (properties) and functions (methods). Both of them are concealed in an object.

The basic idea behind the OOP languages was to combine both data and its member functions into a single unit called Object.

Object is the logical association between program variables and program methods.

Object contains variables that are only accessed by its member functions.

In other words an object is an instance (occurrence) of a class.

Class:

Continuing with the car example, the company which made the car has first designed the car model and its other aspects, they decided the number of gears, they decided how to change the gear, how to accelerate and other methods and properties of the car on just a piece of paper, there is no physical existence of the car yet,

They only designed the car architecture on the paper, which include car properties and car methods, and this architecture of the car is simply called its Class.

Class is a blue print of an object.

Class defines the architecture of an object, it defines the variables and the methods contained in an object.

Class does not have any physical existence, it just defines the object.

Encapsulation:

The word Encapsulation seems to have any biological meaning or it is derived from the medial dictionaries, a new hunter to Oops will think like that, but it is not true.

Data encapsulation and data hiding are the key terms in Object Oriented Programming.

The term Encapsulation means, “The data contained in a variable of an object is only accessed by its member functions”.

If a user wants to access certain data in the object, it will call its member function and that function will read the data and will return the value.

Thus we can assume that the data and its member function is encapsulated in a single unit called object and the data is only accessed by the functions defined in its class.

The Following Diagrams can give you a better approach:

 

opp1

Encapsulation

opp2

No comments: