Object Oriented Programming Explained

This blog post is to demystify about Object Oriented Programming or more commonly known as oops. OOPS is a fundamental concept to know if you want to pursue a career in Software Development.

The formal definition of OOPS is

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

Objects are not typically tangible somethings that you can pick up, sense or feel but they are models of somethings that can do certain things and have certain things done to them. Formally, an object is a collection of data and associated behaviours.

Oriented means directed towards. So object-oriented simply means,”functionally directed toward modelling objects ” . It is one of many techniques used for modelling complex systems by describing a collection of interacting objects via their data and behaviour.

Object Oriented Programming

What is the difference between an object and a class ?

Classes describe objects. They are like blueprints for creating an object.
Eg: We might have three oranges sitting on the table in front of you. Each orange is a distinct object, but all three have the attributes and behaviours associated with one class: the general class of oranges.

Association is the most basic way for two classes to be related.

Attributes and Behaviours

Objects are instances of classes that can be associated with each other. An object instance is a specific object with its own set of data and behaviours; a specific orange on the table in front of us is a said to be an instance of the general class of oranges.

Data describes objects

Data typically represents the individual characteristics of certain object. A class of objects can define specific characteristics that are shared by all instances of the class. Each instance can then have different data values for given characteristics.

For example, our three oranges on the table could have different weight. The orange class could then have a weight attribute. All instances of the orange class have a weight attribute, but each orange might have a different value for this weight. Attributes don’t have to be unique thought; any two oranges may with the same amount. Or two objects representing different customers might have the same value for a first name attribute.

Behaviours are actions

Behaviours are actions that can occur on an object. The behaviours that can be performed on a specific class of objects are called methods. At the programming level, methods are like functions in structured programming, but they magically have access to all data associated with each object. Like functions, methods can also accept parameters and return values.

Parameters to a method are a list of objects that need to be passed into the method that is being called. These objects are used by the method to perform whatever behaviour or task it is meant to do. Return values are the results of the task.

For example; if our objects are numbers, the number class might have an add method that accepts a second number as a parameter. The first number object’s add method will return the sum when the second number is passed to it. Given an object and it’s method name, a calling object can call, or invoke the method on the target object. Invoking a method, at the programming level, is the process of telling the method to execute itself by passing it the required parameters as arguments.

Adding models and methods to individual objects allows us to create a system of interacting objects.

Each object in the system is a member of a certain class. These classes specify what types of data the object can hold and what methods can be invoked on it. The data in each object can be in a different state from other objects of the same class, and each object may react to method calls differently because of the differences in state.

Hiding Details and creating the public interface

The key purpose of modelling an object in object oriented design is to determine what the public interface of that object will be. The interface is the collection of attributes and methods that other objects can use to interact with that object.

Example is the television. Our interface to the television is the remote control. Each button on the remote control represents a method that can be called on the television object. When we, as the calling object, access these methods, we do not know or care if the television is getting its signal from an antenna, a cable connection, or a satellite dish. We don’t care what electronic signals are being sent to adjust the volume, or whether that volume is being output to speakers or a set of headphones. If we open the television to access the internal workings, for expel to split the output signal to both external speaker sand a set of head phones, we will void the warranty.

This process of hiding the implementation, or functional details of an object is suitably called information hiding. It is also sometimes referred to as Encapsulation. The public interface is very important. It needs to be carefully designed as it is difficult to change it in the future. Changing the interface will break any client objects that are calling it. Program objects represent real objects, but they are not real objects. They are models. The model is an abstraction of real concept.

Abstraction is dealing with the level of detail that is most appropriate to a given task. It is the process of extracting a public interface from the inner deitilas. A driver of a car needs to interact with steering, gas pedal, and brakes. The workings of the motor, drive train, and brake subsystem don’t matter to the driver.

Abstraction is the process of encapsulating information with separate public and private interfaces. The private interfaces can be subject to information hiding.

The important to bring from all these definitions is to make our models understandable to the other objects that have to interact with them. This means we should pay careful attention to small details. Ensure methods and properties have sensible names. When analyzing a system, objects typically represent nouns in the original problem, while methods are normally verbs. Do not try to model objects or actions that might be useful in future.

Composition and Inheritance

Composition is the act of collecting together several objects to compose a new one. Composition is usually a good choice when one object is a part of another object.

Example: A car is composed of an engine. transmission, starter, headlights, and windshield, among numerous other parts. The engine, in turn is composed of pistons, a crank shat and valves. In this example, composition is a good way to provide he interface required by a driver, while also providing access to its component parts, which offers a deep level of abstraction suitable for a mechanic.

But the objects in an object oriented system occasionally represent physical objects like people, books, or telephones. More often, however they represent abstract ideas.

A game of chess is played between two players, using a chess set featuring a board containing sixty-four positions in an 8X8 grid. The board can have two sets of sixteen pieces that can be moved, in alternating turns by the two players in different ways. Each piece can take other pieces. The board will be required to draw itself on the computer screen after each turn.

Possible objects in italics and a few key methods using bold.

Aggregation is almost exactly like composition. The difference is that aggregate objects can exist independently. If we take the chess example again, it would be impossible for a position to be associated with a different chess board, so we say the board is composed of positions. but the pieces, which might exist independently of the chess set, are said the be in an aggregate relationship with that set.

Another way to differentiate between aggregation and composition is to think about the lifespan of the object. If the composite (outside) object controls when the related (inside) objects are created and destroyed, composition is most suitable. IF the related object is created independently of composite object, or can outlast that object, an aggregate relationship makes more sense. Composition is aggregation; aggregation is simply a more general form of composition . Any composite relationship is also an aggregate relationship, but not vice versa.


The is a relationship is formed by inheritance.

For example, there are thirty-two chess pieces in our chess set, but there are only six different types of pieces (pawns, knights,rooks, bishops, king and queeen), each of which behaves differently when it is moved. All of these classes of piece have properties, like colour and the chess set they are part of, but they also have unique shapes when drawn on the chess board, and make different moves.

Inheritance provides abstraction

Polymorphism is the ability to treat a class differently depending on which subclass is implemented.

When we think of inheritance in our own family tree, we can see that we inherit features from more than just one parent. When strangers tell a proud mother that her son has, “his fathers eyes”, she will typically respond with the lines of, “yes, but he got my nose”.

Object-oriented design can also feature such multiple inheritance, which allows a subclass to inherit functionality from multiple parent classes. In practice, multiple inheritance can be tricky business, and some programming languages(most notably, Java) strictly prohibit it. But multiple inheritance can have its uses. most often, its used to create objects that have two distinct sets of behaviours. For example, an object designed to connect a scanner and send a fax of the scanned document might be created by inheriting from two separate scanner and faxer objects.As long as two classes have distinct interfaces, it is not normally harmful for a subclass to inherit from both of them. But it gets messy if we inherit from two classes that provide overlapping interfaces.

In future blog posts I will explain how to implement oops concepts in python.

Please post doubts in the comment section.

Leave a Reply