Tuesday, February 23, 2010

Object Oriented Programming: The Object

The Object

What is an Object?

To define objects we have to define two things:
·         State (Attributes, properties)
·         Behaviour (Methods)

Real-world objects share two characteristics: They all have state and behaviour. Dogs have state (name, colour, hunger) and behaviour (barking, fetching, wagging tail).

Real-world objects vary in complexity; a desktop lamp may have only two possible states (on and off) and two possible behaviours (turn on, turn off), but a desktop radio might have additional states (on, off, volume, station) and behaviour (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

The term “Object,” that gives OOP its name, refers to a conceptual object that represents an item in our program or system. This could be anything from a button on a web page or a computer file, to a real world object such as a car.

Software objects like real-world objects also consist of state and related behaviour.
·         An object stores its state (Attributes) in fields or variables
·         An object exposes its behaviour 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.

Hiding the internal state and methods of a class through abstraction and requiring all interaction to be performed through an object's methods is known as encapsulation.

Bundling code into individual software objects provides a number of benefits, including:
·         Modularity: The source code for an object can be written and maintained independently of the source code for other objects. If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
·         Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program.

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.