Object-Oriented Programming or OOP is one of the must-know foundations when you learn a programming language like Java, C++, or Python (very widely used for Data Science) because these languages are based on the concept of OOP. Coined by Alan Kay around 1966 or 1967, its history is long forgotten but the concept still plays its role in being one of the most useful paradigms based on the concept of ‘objects’.
In this article, we’ll touch upon the history of OOP, what it is and which languages use OOP today, the principles of Object-Oriented Programming followed by a complete working example from Python.
Also, read -> what is a decorator in python and how to use it?
History of Object-Oriented Programming
Object-Oriented Programming can be traced back to 1962 with the first instance of OOP being introduced with the language SIMULA 1 and Simula 67 followed up with the same concept in the year 1967. The work done on them was essentially by two researchers from the Norwegian Computing Center in Oslo, Norway, namely, Ole-John Dahl and Kristen Nygaard.
While it was introduced to the world as early as 1962, Object-Oriented Programming only gained pace after the development of more languages with the concept, and only after the 1990s did it gain its much-needed push to be cited to be an important concept in programming and it grew thereafter.
What is Object-Oriented Programming or OOP?
Object-Oriented Programming is the way by which programmers can create their own objects that have their own methods and attributes to be used in their code. For example, in Python, one can define a list, string or dictionary, and other objects and then be able to use methods that can be called off of these objects with syntaxes that look like string.method_name(), etc.
Example: If you had to make an analysis that consists of repeatedly using a formula to calculate the circumference and area of a circle, the area of a rhombus, and the area of a square. Either you can include the formula repeatedly by defining it in a function in Python or you can include all of them in a class named shapes with multiple objects like circle, rhombus, and square to help keep all the required formulas in a single class which you can use and call out in the code whenever you desire.
It is a little confusing to use at first and the uses might not really be very evident but someone who knows how to use OOP, they can create code that is repeatable easily and highly organized. Consider if you have a really long script to write in Python, it is not very possible for you to only use in-built functions to organize the code effectively and repeatedly use it. OOP is essential because:
- Helps organize larger code and make it reusable
- Can help in maintaining different methods with common names under separate objects
- The class can be changed and altered to add new methods as required which is not so easy in a conventional programming language
- OOP helps break down a problem into different chunks called objects without having to manually assign all the variables
- The object-based approach is more real-life with consideration being given to the inheritance concept
- It helps you only show the code that is necessary without having to expose the implementation code as in conventional programming
Commonly repeated tasks and objects tend to be more defined with the OOP when the code is used to create a repeatedly usable process or method. For an example in python:
class NameOfClass(): def __init__(self, param1,param2): self.param1 = param1 def some_formula(self): #to perform a formula unique to your analysis print(self.param1)
Which languages use Object-Oriented Programming today?
The following languages use OOP today;
- Visual Basic
- MATLAB etc.
Find the full list here: List of Languages using OOP
Principles of Object-Oriented Programming
There are four primary principles of Object-Oriented Programming:
Encapsulation helps the language to keep the objects hidden and the implementation of and state of each object is a privately held affair within each class. Other objects in the language don’t have the authority to manipulate the class. The information is hidden and only parts of it are revealed to ensure that the data hiding helps in greater program security and helps avoid any corruption from other objects in the language.
For example: Suppose your code has elements to print the calculated values in a certain way, say if you want the height in cms to be multiplied by 0.4 to give the output as inches, you can hide the multiplication code and can reduce your work and hide the implementation code and only give the output when the method is called without having to expose the implementation which can be corrupted, wherein our example someone can manipulate the value of 0.4.
Abstraction reveals only the mechanisms that the code executes which are required to be known to the programmer and not any unnecessary implementation codes. The class can have its functionalities extended and the developers can make the changes as required to the code within the class and avoid any extra noise around the code to find it easily accessible. For example, using the method names which start with an underscore in python lets the method remain ‘private’ and can allow the access restrictions to be put in explicitly on these methods.
For example, You can assign private methods like assignment of default values, etc., which cannot be changed by an end-user after you hide the implementation code in a class. This helps in not showing the user all the code in the class and abstracting it by placing explicit restrictions wherever required.
Inheritance is one of the biggest advantages of Object-oriented programming where the relationships and subclasses of one class of code can be reused in another class. This communication between classes to call for codes from other classes will help with better accuracy and lesser workload and it works while still ensuring that the hierarchy between the parameters and classes is still maintained. The inheritance principle also reduces the problems that come with too many classes and parameters by allowing the usage between different classes.
For example, Inheritance helps in making a relationship between the objects and their sub-classes or sub-entities by defining a hierarchical relationship. For example, dogs and cats are inherited sub-classes of an object or class named ‘pets’.
Polymorphism in Object-oriented programming allows the objects in the classes to take on different ways of execution. The code allows subtyping – which lets the code be called independent of which class in the supported hierarchy it is operating on – the parent class or one of its descendants.
For example, objects of type dog and cat are of the same class called ‘pets’ and the functions common to each element will implement what is necessary to be called while itself being indifferent to which pet is being called.
For example: if you want to call a code from a class ‘Pets’ i.e. the ‘object’ Dog in a different class named ‘Kennel’, you can do it using OOP’s Polymorphism principle.
Difference between OOP and Conventional programming
With conventional or traditional programming you will have to divide the problem into functions and with OOP you can divide the same problem into a number of objects with their own relevant data. The area is common to a shape like a circle or a square but in the conventional method, you will have to redefine the function for a square after you define it for a circle.
In Object-Oriented Programming, you can define the area for each shape separately without having to call it by different names. The approach is more like a parent-child approach which is how it works in the real world with each entity having its own sub-entities which is not the case in conventional programming.
Object-oriented programming is more suitable, in case a very long and really large complex system of code is being used and maintained wherein the classes are organized and the code can be reproduced as required but in conventional programming, the time consumption and complexity of maintenance of large systems are not feasible.
An example to understand OOP perfectly
If you did not use OOP you would probably go about doing a code in the following way
area = radius * radius * .pi radius = 2 pi = 3.14 area #this is the conventional method where you assign values to variables and expect your program to find #what it should do every single time you assign a variable #and you can observe that the variables will have to be reassigned to execute it again #like radius = 3 area radius = 10 area
Imagine if this is the case with using multiple formulas like area, circumference, and not just for a circle but redefined area for more shapes like squares, etc. This can be solved with using an OOP where each object has its own separate data like:
An object like a plane will have its own data like wings() and fly(), etc.
Using OOP you can write the same code with different methods under each class as follows:
class Circle: pi = 3.14 # Circle instanted with radius 1 def __init__(self, radius=1): self.radius = radius self.area = radius * radius * Circle.pi #calling object pi from class circle # Reset Radius def setRadius(self, new_radius): self.radius = new_radius self.area = new_radius * new_radius * self.pi # Get Circumference def getCircumference(self): return self.radius * self.pi * 2 circ = Circle() print('Radius is: ',circ.radius) print('Area is: ',circ.area) print('Circumference is: ',circ.getCircumference())
In this example of Object-Oriented Programming, we’ll go about defining a class ‘Circle’ with multiple methods under it.
As you can see by default the first variable being assigned in the class is ‘pi’ to be used in the different methods in the OOP.
The objects are then formed like the method for finding area and the radius is set at a default one as can be seen in “def init(self, radius = 1)” and then the area is defined as twice the radius multiplied by the object ‘pi’ as defined in the class to be 3.14
The reset radius method is used to set a new radius in the function and then the area is found as twice the times of the new radius multiplied by Pi again.
Another method under this same class is defined to find the circumference of the circle.
As you can see how easy it is to define multiple functions in the class and use them repetitively, you can also use an inbuilt value like ‘e’ or the Euler constant which is a globally assigned variable in python in the class without having to explicitly define it.
Object-oriented programming is one of the most essential basics that you should learn when you start out with a programming language like python or R. The programming languages require a lot of attention and knowing these foundations will only help you make coding simpler and easier to use. The larger scripts and reusability of code will only help in making your code easier to execute. The OOP feature of python and R is useful if you work in a particular industry if you have your own particular analyses to conduct and particular formulas or functions to use that might not be a part of an inbuilt package in Python.
For more such content, check out our website -> Buggy Programmer