Search This Blog

2010/01/17

Javascript:Principles of Object Oriented Programming

What is Object Oriented Programming?
Each Computer Program consists of code & data. When we organize a program
in such fashion that it seems that code act on data. this is the premier
way we used to program in C.This way is called procedural way but when
size of code grows beyond a certain limit it becomes hard to manage.

Second approach is to organize code in way that program consist of well
thought out entities (read object) and each entity has it’s own attributes
& method to manipulate that attribute it can be roughly called an Object
Oriented programming(OOP).

Definition:
Object-oriented programming (OOP) is a programming paradigm based on
the concept of objects,which can contain data and code: data in the
form of fields (often known as attributes or properties), and code in
the form of procedures (often known as methods).

OOP equally govern by a common principle of code reuse.

BASIC Principles of OOP
There are four basic principles that can describe OOP as whole. Those
principles are as follows.

1) Abstraction:
Abstraction can be thought out as an act of emulating a real world
object in programming world. e.g. we can define customer entity in
programming world as Abstract representation of a customer in real
life.Abstraction can be achieved by composition. In real world an
object can further thought out as composed of many constituent object.
e.g. Car can be thought out as composition of engine,gearbox, steering,
sound system etc. Looking at car as single object undoubtfully help to
drive car better than thinking it as many object interacting with each
other simultaneously. We can delve into inner object only when we need
to do so until then we think of a car as single unit which provided us
methods to drive by steering wheel, breaks to stop car, feel fuel in
fuel tank to keep car running.Abstraction can help us to keep
complexity at minimums.

Abstraction in the context of computer science and software engineering,
refers to the concept of hiding complex implementation details and
exposing only essential functionalities or characteristics of a system
or an object.

2) Encapsulation:
Encapsulation can be thought out as an act of putting protective
wrapper around code & data in program so that client program can’t
access the server programs code and data arbitrarily, but it should
follow the well-defined Interface defined by server program. This
helps to keep server program’s code and data secure. Further it mask
client program from the way actual Implementation of interface in
server program.The server program can modify the Implementation
of interface keeping same method of accessing it, the client Program
doesn’t need any modification even if the server program change
actual Implementation of interface in it.Encapsulation defines access
rule (levels Public/private/protected/friend/internal/) for accessing
Program’s Code & data Applicable to client program as well as it’s
own constituent units.

Encapsulation hides the internal state and implementation details of
an object from the outside world, and to provide controlled access to
the object's properties and behaviors through well-defined interfaces.

3) Inheritance:
Inheritance can be thought out as a process by which an object acquires
the attributes,behaviour of another object being special case of it.
e.g. each animal has common behaviour like eating food, breathing,
sleeping,speak,reproducing new offspring etc, attributes like
ability to wander (plant can’t move on themselves) etc. Hen is special
case of animal that inherits all these attribute & behaviors but it
may contain its own attributes & behaviors that are not common to all
animals e.g. it can fly. Inheritance helps to prevent un-necessary
reductant coding.

4) Polymorphism:
In linguistic sense polymorhism is made up of (Poly-morph-ism).
Polymorphisms is made up of Greek words poly-means more than one,
morph means form. So polymorphism can be thought out as an Act of
existing in more than one form.e.g we behave differently to same
stimulus at different situation.

In programming, polymorphism refers to the ability of a single
interface (such as a method or function) to handle multiple data
types or forms. This is achieved through mechanisms like method
overloading (where multiple methods with the same name but different
parameter types are defined) or operator overloading (where operators
like + or - behave differently based on the operands' types).

In example of finding the average of two numbers (either integers or
real numbers) using the same interface demonstrates compile-time
polymorphism. When you call the average function with integers, the
corresponding implementation for integer arithmetic is invoked.
Similarly, when you call it with real numbers, the implementation for
real number arithmetic is executed. The decision on which
implementation to use is made at compile time based on the types of
the arguments passed.

Dynamic polymorphism, which is achieved through inheritance and method
overriding in object-oriented programming. In dynamic polymorphism,
the actual method implementation that gets executed is determined at
runtime based on the object's type, allowing for more flexibility and
extensibility in code. Difference Between Abstraction and Encapsulation
Abstraction Encapsulation
Abstraction is a feature of OOPs that hides
the unnecessary detail but shows the essential information.
Encapsulation is also a feature of OOPs.
It hides the code and data into a single entity or
unit so that the data can be protected from the outside world.
It solves an issue at the design level. Encapsulation solves an issue at implementation level.
It focuses on the external lookout. It focuses on internal working.
It can be implemented using abstract
classes and interfaces.
It can be implemented by using the
access modifiers (private, public, protected).
It is the process of gaining information. It is the process of containing the information.
In abstraction, we use abstract classes and interfaces to hide
the code complexities.
We use the getters and setters methods to hide the data.
The objects are encapsulated that helps to perform abstraction. The object need not to abstract that result in encapsulation.

No comments:

Post a Comment