Object Oriented Programming Introduction

by - posted

oop introductionIntroduction

The idea of the Object Oriented Programming Introduction is to describe the basic architecture and the elements of OOP.
OOP terms will be explained and OOP will be compared with procedural programming as well. Finally, we show some basic examples of OOP in Python.


Object Oriented Programming (OOP) can be seen as an extension of procedural programming.
It may be seen as the design of software using a collection of cooperating objects. In a traditional view, a program may be seen as a collection of functions or more simply as a list of code.

Object oriented programming is a method of programming, based on a hierarchy of classes and
cooperating objects. Classes and objects are the two main components of object oriented programming.

Classes are the structure or concept of an object (blueprint, template). Classes can be used to generate multiple instances. Such instances are called objects.

Objects are used to design programs and interact with each other.

An object :
– can be viewed as an independent machine
– is an abstract data type with the addition of polymorphism and inheritance
– is a particular instance of a class
– is capable of receiving messages, processing data and sending messages to other objects

Classes and Objects haves 2 parts :

1) Methods

— describes how it can be used or what it can do
— Methods are blocks of code
— Methods have behaviors

— Methods are also called : functions, procedures

2) Properties

— describe it’s characteristics
— Properties are variables (data)
— Properties have states

— Properties are also called : variables, fields, data fields, members, data members, data structures, attributes


A class defines the structure, the blueprint of an object.

Much of the art of object oriented programming is determining the best way to divide a program into an economical set of classes.
Classes are a way of aggregating similar data (properties) and functions (methods). A class is basically a scope in which various functions are executed.
An object constructed by a class is called an instance of that class.


An object contains functions (methods) and data stored in variables (properties).

An object is a “black box” which receives and sends messages. Everything an object can do is represented by its message interface. So you shouldn’t have to know anything about what is in the object in order to use it.
Variables that belong to an object are called properties. Proprieties can belong to each object of the class. They are also called instance properties.
Objects can also have functionalities by using functions that belong to a class. Such functions are called methods of the class.


An interface emphasises the idea of abstraction – by suppressing the details of the implementation.
An interface defines a set of ways to interact with an object. With an object’s interface, you can use the ways, specified in the interface, to interact with this object.

Message passing

Message passing architectures are systems where each component is independent of the other, with a common mechanism for passing data between them.
Message passing in OOP is the process in which an object sends data to another object. This can be inside the same program or to an object in another program.

The 4 central principles of OOP

1. Encapsulation

Encapsulation (or information hiding) is about hiding complexity. In the real world, objects frequently hide their information and how they work. You don’t need to know the internals in order to use an object. Using an interface to interact with an object is an example of hiding complexity.
Encapsulation in OOP is combining code and data as a single unit. It is achieved by using the class concept. The idea of encapsulation is to hide how a class does its business, while allowing other classes to make requests of it.

2. Abstraction

The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects. Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.

3. Inheritance

When you create a subclass you inherit all the parent methods and properties of the original class. You can also add new methods and properties to the subclass.
A new class inherits all the existing messages and therefore all the behavior of the original class. The original class is called the parent-class, or super-class, of the new class.
Inheritance also promotes reuse. You don’t have to start from scratch when you write a new program. You can simply reuse an existing class that has behaviors similar to what you need in the new program. The explanations above are also valid for objects.

4. Polymorphism

In object oriented programming, polymorphism (from the Greek meaning “having multiple forms”) is the characteristic of being able to assign a different meaning or usage to something in different contexts.
Polymorphism allows objects to be represented in multiple forms. Even when classes are derived or inherited from the same parent class, each derived class will have its own behavior. Polymorphism is a concept linked to inheritance and assures that derived classes have the same functions even when each derived class performs different operations.

Comparison of OOP programming and procedural programming

When programming, you are essentially dealing with data and code. The code will use and change the data.
This aspect of programming is handled quite differently in procedural programming compared with the object oriented approach. These differences require different strategies in how we think about writing the code.


The focus of procedural programming is to break down a program into a collection of functions.
The code is organized into functions that use and change our data. These functions typically take some input, do something, then produce some output. Ideally functions would behave as black boxes where input data goes in and output data comes out.
The key idea here is that functions have no intrinsic relationship with the data they operate on.
Sometimes functions need to access data that is not provided as a parameter, so they need access to data that is outside of them. Data accessed in this way is considered global.

Object oriented

Object oriented programming breaks down a programming task into objects that expose functions (methods) and data (properties).
In object oriented programming, the data and related functions are bundled together into an object. Normally the data inside an object can only be manipulated by calling the object’s functions. This means that the data is locked away inside objects and the functions provide the only means of doing something with that data. In a well designed object oriented program, objects never access shared or global data, they are only permitted to use the data they have.


While procedural programming uses functions to operate on data structures, object oriented programming bundles functions and data structures together. An object operates on its own data structure.
Procedural programming makes use of shared and global data, while object oriented programming locks its data away in objects.

The nomenclature varies between the two, although they have similar semantics :

Object oriented     Procedural
class                       xxxxxx
object                     module
method                  function
properties             variables
message               function call

OOP Python examples

Example 1 (without parameter)

Define a class
class MyClass():
here is the place for methods / properties
# end

This is the structure of a class. The first Character of the name of the class is by convention a capital letter.

Define a method
def sayHi(self):
print('Hello world')
# end

This is a method, without parameter. Methods have only one specific difference from ordinary functions namely the “self” parameter.

Create an object (of type MyClass)
instantiate an object (object = instance of a class)

p = MyClass()

Create a new object of class “MyClass” and assign it to the variable “p”. Now you can access/call all the methods and properties of this class via this variable.

Access the object method

Execute the method sayHi() – without a parameter – of this specific object of the class “MyClass”.
“sayHi” is the attribute of the object “p”.

Get the result
Hello world

The code

class MyClass():
def sayHi(self):
print(‘Hello world’)
# end method
# end class
p = MyClass()

Example 2 (with parameter)

class Person():
def sayHi(self, name):
self.name = name
print(‘Hello, is that your name ?’, self.name)
# end method
# end class
p = Person() # creates an empty object
p.sayHi(‘Smith’) # set the property (*) of the object to “Smith”
r = Person() # object 2

(*) property means data

The __init__ method

This method is useful to do any initialization you want to do with your object.

The __init__() constructor is used (runs) to initialize variables or execute class methods when an object is first created / instantiated.

Example :

When creating a new object the arguments in the parentheses – following the class name – are automatically processed by the __int__ method.
The __init__ method takes the parameter name (along with the usual self). We also created a new field called name. These are two different variables even if they have the same name.

Example 3 (init 1)

class Person():
def __init__(self, name):
self.name = name
# end
def sayHi(self):
print(‘Hello, is that your name ?’, self.name)
# end
# end class
p = Person(‘Smith’)
r = Person(‘Bush’)

Example 4 (init 2)

class Person():
def __init__(self):
self.name = “Marc Schwager” # set default property
print “—- initialization is ok —-” # print a confirmation message
# end
def setName(self, name): # method to change the object property
self.name = name
# end
def sayHi(self): # displays the current value of the objects property
print(‘Hello, is that your name ?’, self.name)
# end
# end class
p = Person() # displays “—- initialization is ok —-”
p.sayHi() # displays the default property
p.setName(“John Smith”) # alter the default property
p.sayHi() # displays the new/altered property

Example 5 (init 3)

class Person():
def __init__(self, name=”Marc Schwager”): # add argument/parameter + specify default value
self.name = name
print “—- initialization is ok —-”
# end
def setName(self, name):
self.name = name
# end
def sayHi(self):
print(‘Hello, is that your name ?’, self.name)
# end
# end class
p = Person() # displays “—- initialization is ok —-”
p.sayHi() # displays the default property/value
p = Person(“John Smith”) # alter the default property/value
p.sayHi() # displays the new/altered property/value
p.setName(“Mike Brown”) # alter the property/value
p.sayHi() # displays the new/altered property/value


Class is a box with associated variables (properties) and functions (methods) a blueprint of an -> Object

Object is built out of a -> class

Proprieties are data, variables, members

Methods are code, functions, procedures

Interface is a set of rules to interact with an object

Message passing is a common mechanism for passing data between independent elements

Encapsulation is about hiding complexity

Abstraction is about hiding irrelevant details

Inheritance is about receiving properties from the inheritor

Polymorphism allow OOP elements to have more than one form

Composition is one large object that is composed of several other predefined objects

If you enjoyed this article, you can :

– get post updates by subscribing to our e-mail list

– share on social media :