Thursday, May 21, 2009

OOP Concepts

I know every programmers know the OOP concept but still I have decided to write small and sweet article related to OOP used to refresh our OOP concepts.

Definition:

OOP is the programming model where related information (data/fields-properties and processes/method) are integrated to form an object.

Advantages:

- Code redundancy and maintenance is lower.
- Code readability (Abstraction), reusability (Inheritance), reliability, security (Encapsulation) is higher.
- Less coding
- In OOP, everything (data and process) is related to object and object is closely related to real life.

Disadvantages:

- Performance/efficiency is lower then Structured Programming

Reason:
1. Object is reference type and referenced through pointer. So little bit of space overhead to store pointers and little bit of speed overhead to find the space in the memory (heap) to store the objects runtime.
2. For dynamic methods there is little bit of time overhead to call the right method while OOP features used like inheritance, polymorphism.

Features:
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

Abstraction:

A concept without thinking/showing of a specific example/actual implementation. It is the process to hide/omit unwanted detail and showing just representation of the actual implementation/detail.

Abstraction refers to the act of representing essential features without including the background details or explanations.

e.g. Real time examples are,
- Stopping the car engine, starting the car engine, switch on/off to any electrical devices.

Abstraction is achieved by classes and objects.

Encapsulation:

It is the process of integrating the data and functions into a single unit called class.
The objective of encapsulation is to provide a).Protection and b).Information Hiding.

a).Protection is the limiting and use of class’ data and functions. In short, data and/or functions can not be used with out object. Protection is about adding methods and data to a class. When you add methods or data to a class, then you are protecting the methods or data from use without first having an object of the class.

b).Information hiding is the removing the data and functions from public space of class. It can be achieved using access modifiers like private, public, protected.

Abstraction tells us what external face we should present to the world where as Encapsulation ensures that the implementation of the interface doesn’t leak out to the outside world.

Inheritance:

It is the process to inherit/derive commonly used data (state) and functions (behavior) from other classes, called parent/base class and inherited/derived class called child or derived class. It is intended to reuse existing code with little or no modification.

a).Generalization and b). specialization are the associated with the concept of inheritance.

a). Generalization is the defining the classes, called base/parent/super class, having common characteristics of its subclasses.
e.g. “Vehicle” is the generalization of “car”, ”bike”, ”plane”, ”boat”, ”bicycle” and many more.

b).Specialization is to refine the derived/child class behavior using overriding.
e.g. Start and Stop behaviors of vehicle are different. Car has different mechanism to start then bike.

Polymorphism:

Poly – multi and Morph =”forms” -> having multiple forms
Polymorphism is the process of using an operator or function in different ways for different set of inputs given.

Redefining the inherited the members differently using overloading and overriding is called polymorphism

There are 3 concepts to achieve polymorphism;

1. Function overloading – functions having same name but different signatures/prototype does not include return type
e.g. Add(int a, int b) - Adds two integer numbers
Add(string a, string b) - concatenates the string a & b
2. Operator overloading – redefining existing operator for the objects of the classes.
e.g. + used to add numbers
+ used to
concatenates two string
3. Function overriding - redefining the derived method into the derived class.
e.g. Work() - defined in Parent class - Parent do business
Work() - defined in Child class - Child does the job

No comments:

Post a Comment