Wednesday, February 17, 2010

Principals of Object Oriented Programming (OOP)



I decided to write this as I remember how hard it was to get my head around the principals of Object Oriented Programming (OOP) when I made the jump from procedural (or sequential) programming in C to using OOP in  C#.
I think some programmers underestimate how big a leap this is when someone say from an engineering background who learned to program in a procedural language has difficulty with the principals of OOP. This is mainly because it just isn't how we learned to think about programming and requires having to relearn our approach to solving our programming problems.


Object oriented programming (OOP) is a programming model that uses the concept of breaking programs or assemblies into a collection of smaller more manageable Objects or building blocks thus keeping projects simple and promoting improved code reusability and maintainability.

A Class (classification - class) represents the definition for an object. A class is the generic definition of what an object is - a template. A term unique to OOP, instantiation, is simply the act of creating an instance of a class. That instance is an object.

A summary of the principals that define OOP are:

Encapsulation:
Binding related data and functionality in an object is called data encapsulation and allows the user to hide the information/behaviour of the object from the outside world using abstraction.

Abstraction:
Abstraction is the process of hiding all but the relevant data about an object in order to reduce complexity and increase efficiency.

Inheritance:
In OOP, a parent or base class can inherit its behaviour and state to children or derived classes. Inheritance gives you the ability to build new classes based on an existing class. You can then extend a base class by enabling a new class to inherit its characteristics and behaviour.

Polymorphism:
Poly meaning many, and morph meaning forms, literally many forms. Polymorphism allows objects to be represented in multiple forms. Even though classes are derived or inherited from the same parent class, each derived class will have its own behaviour. Polymorphism is a concept linked to inheritance and assures that derived classes have the same functions even though each derived class performs different operations.




No comments: