What is Object Oriented Programming?

Object Oriented Programming languages organise programs based on objects and their interactions.

Examples include: SmallTalk, Java, Objective C and C++

Here is a list of the main Object Oriented principles:

  • Class
  • Object
  • Inheritance
  • Message Passing
  • Encapsulation
  • Polymorphism
  • Dynamic Binding
  • Dynamic Typing
OOP, Object Oriented Programming

Class

Is the definition of an object’s behaviour (methods) and properties (attributes).

Template or blueprint that defines the behaviour and properties

Method = Function = Behaviour = Action

Property = Attribute = Data Type

Object

An object is an instance of a class.

A class can have many instances.  All these instances are said to be the member of the same class.

Objects have methods and instance variables.

Each object or instance of a class has memory allocated for its own instance variables.

In the following example, Joe and Mary are instances of class Person.

OOP Example

Inheritance

In this example Class B, Class C and Class D inherit from Class A.

In other words Class A is the superclass or parent class of Class B, Class C and Class D.

Class B, Class C and Class D are known to be a subclass or child class of Class A.

In inheritance, the subclass or child class inherits all the instance variables and methods of its superclass or parent class.

The sublass/child class can then add its own instance variables and methods.

OOP Inheritance

There are two types of inheritance:

Single Inheritance

A class can only one direct superclass.

Multiple Inheritance

A class can inherit from more than one class.

Message Passing

In Object Oriented Programming a program is based on objects and their interaction with each other.  Objects communicate with each other through messages, which are requested to perform methods.

You send a message to an object, requesting it to perform one of its methods.

Encapsulation

Encapsulation is also known as information hiding where the internal representation of on object is hidden.

It is the separation of the implementation of an object from its interface.

Methods of an object wrap around its instance variables and hide them from the rest of the program.

One of the advantages of encapsulation is that the implementation of a class can be changed without affecting the code that calls it.

Polymorphism

Polymorphism means many forms.

This is the mechanism by which different classes can define methods with the same name.

Different receivers can have different implementation of the same method.

The result of a message will depend on the object that receives the message.

Polymorphism

Dynamic Binding

In dynamic binding, the method to invoke is determined at runtime instead of compile time.  Also referred to as late binding.

Dynamic Binding enables polymorphism where method to run is determined at runtime.

Example:

Shape is the superclass of Circle, Square and Rectangle and these classes all have a draw() method and the exact method to run is determined at runtime.

Dynamic Typing

A variable is dynamically typed when the type of the object it points to is not checked at compile time.

Objective-C uses the id data type to represent a variable that is an object without specifying what sort of object it is. This is referred to as dynamic typing.

OOP Concepts