Wednesday, October 08, 2008

OOP Basics

Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. Programming techniques may include features such as encapsulation, modularity, polymorphism, and inheritance.

Why OOP?

  • It bridges the gap between problem and solution domain.
  • Lets you decompose problem into smaller sub-problems that can be solved separately.  Modularity.
  • Enables reuse

Abstraction

  • Abstraction, as a process, denotes the extracting of the essential details about an item, or a group of items, while ignoring the inessential details.
  • Abstraction, as an entity, denotes focused representation for an actual item.

We can have varying degrees/levels of abstraction.
Abstraction is most often used as a complexity mastering technique.
As we move to higher levels of abstraction, we focus on the larger and more important pieces of information.

Encapsulation

  • Encapsulation, as a process, means the act of enclosing one or more items within a (physical or logical) container.
  • Encapsulation, as an entity, refers to a package or an enclosure that holds (contains, encloses) one or more items.

The walls of the enclosure may be "transparent," "translucent," or even "opaque.
Everything that is encapsulated is not hidden.

Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden.
Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

Inheritance

Inheritance is a way to form new classes using classes that have already been defined. The new classes (derived classes), take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes. It is intended to help reuse existing code with little or no modification.

Inheritance is also sometimes called generalization, because the is-a relationships represent a hierarchy between classes of objects.

If the derived class adds new interface elements to a derived type, it can still be substituted for the base type - but the substitution is not perfect. This can be described as an is-like-a relationship

We can have object of another class inside our new class, this concept is called composition/aggregation. Composition is often referred to as a "has-a" relationship.

Composition should be favored over inheritance.

Polymorphism

Polymorphism is the ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behavior. The exact behavior is determined at run time (this is called late binding or dynamic binding).

Compile time polymorphism : function and operator overloading.
Run time polymorphism : generics and overriding.

Templates

Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

No comments: