Google
Information Storage and Retrieval: September 2011

Pages

Monday, September 19, 2011

Object Oriented Programming: Some Basic Concepts

All computer programs consist of 2 parts: code and data. So, a program can be conceptually organized around its code or around its data. So, there are 2 approaches of writing computer programs: Programs that are written around "what is happening" and programs that are written around "who is being affected". The first approach is known as process-oriented approach. Procedural languages like C follow this model. The second approach is known as 'object-oriented' approach. Languages like C++ and Java follow this approach.

Object -Oriented approach helps in managing complexity better as it is more similar in nature to as how humans manage complexity. Human beings manage complexity through abstraction. e.g people do not think of a car as a set of thousands of individual parts or as a set of thousands of different processes that undergo in running a car. Instead they look at it as a well-defined object with its own unique behaviour. People can easily ignore the complex details of  how the engine, transmission and breaking systems work, while they drive a car. So a car can be visualized as a single object containing of several subsystems(with each of its unique behaviour) like steering, breakes, sound-system etc. In a similar fashion, a program can be written wherein data can be transformed by abstraction into its component objects and each of these objects describes its own unique behaviour.

Every object has state and behaviour. e.g Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). In similar fashion, software objects  too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Encapsulation:

Encapsulation is a mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. Class is an example of encapsulation.

Inheritance:

Inheritance is the process by which one object acquires the properties of another object. So a class can have a subclass. The subclass will inherit the state and behaviour of its parent class plus it can have its own state and behaviour.

Polymorphism:

Polymorphism is a concept that allows one interface to be used for a general class of actions. e.g if in a program you need 3 differnt kind of stacks: 1 used for integers, one for characters and one for floating point numbers, then you can use 3 methods with the same name.