Intro to Object-Oriented Programming

pythonobject-oriented programming

Friday, November 3, 2023

Object-oriented programming (OOP) is a software design principle that utilizes a combination of variables, data structures, and functions to create representations of real life objects and their characteristics. The idea behind OOP is to abstract data into smaller, manageable components to organize code and minimize the complexity of a program.

Object oriented programming was first conceptualized by computer scientists in the 1960s. In 1967, Ole-Johan Dahl along with Kristen Nygaard developed the first object-oriented programming language, Simula. Simula introduced the concept of classes, objects, inheritance and polymorphism, which are all core features of OOP today.

Another pioneer in the field of OOP was Alan Kay, one of the creators of the programming language Smalltalk. In Smalltalk all values are objects — numbers, data structures and even classes. Smalltalk also introduced the model-view-controller(MVC) design methodology — built on the foundation of OOP principles. This method was instrumental in the development and advancements of graphical user interfaces(GUIs) as we know them today.

In the 1990s, as GUIs grew in popularity and more programming languages emerged that supported OOP principles, OOP became one of the major programming paradigms used and taught across the world.

Today there are many programming languages that support object-oriented design principles including Javascript, Java, C++ and Python. These languages are not restricted to just OOP, in fact many programs are built using a combination of programming styles. The focus of this article will be to explore OOP principles with examples written in Python.

One thing to note before going further is that Alan Kay’s original vision for OOP is different than the current implementation of OOP. Much can be said about the fundamental differences, but in essence, he envisioned objects as mini-computers inside of a larger computer that were completely autonomous with the ability to render themselves, communicate over a network and persist their own state. Although there are overlapping concepts, OOP in application today typically refers to the concept of leveraging classes for the abstraction of data. This article will be discussing the latter.

Key Principles of Object-oriented Programming

Key principles of Object Oriented Programming Image

Abstraction

Abstraction is the process of utilizing classes and objects to hide information or processes from the user and only providing information that is relevant to the particular task.

Encapsulation

Encapsulation refers to the process of organizing objects and their methods in one place by utilizing classes as templates for the behavior and characteristics of objects. The code inside of these classes model the behavior of objects with other objects and can be used to control the way in which the attributes and properties of an object are updated.

Inheritance

Inheritance is the ability of new subclasses to inherit some or all of the properties of an existing class.

Polymorphism

The definition of polymorphism is numerous forms. In OOP polymorphism is the concept of expressing common behaviors between objects. Classes can inherit behaviors(methods) from each other but have the ability of expressing those behaviors differently through overloading / overriding.

Classes, Objects, Attributes and Methods

OOP utilizes classes to create blueprints for the creation of objects, also referred to as instances. In Python, a class is created by using the keyword class. Classes contain information about the characteristics and behavior of an object.

Attributes are the characteristics of an object and methods are functions that control the behavior of an object.

Suppose you were building out a program for a veterinary’s office to manage their pets. Using OOP design principles, you may create a Pet class, with attributes that define particular characteristics of each pet.

In this case, a pet would have attributes such as name, weight, owner and age. Python uses special methods that work in the background to enhance the functionality of classes. The special method init is called upon during an object’s creation, allowing you to assign attributes upon an object’s instantiation.

using init method to create Pet class

To create an instance representing an actual pet, you could define a variable and assign it to the Pet class called with the required arguments, similar to calling a function. Once you create an instance of the Pet class, you can access and update that object’s attributes using dot notation.

adding attributes to Pet class

Methods are functions within a class that define behaviors of an object. Methods can return information about an object, perform actions, or update an object’s state.

Returning to our Pet class example, a veterinary clinic may want to keep track of a pet’s vaccinations. We could create a method within our class, called vaccinations to keep track of a pet’s vaccinations.

To do so, we could define a method vaccinations, pass in the arguments of self, and vaccine. To keep track of a pet’s vaccinations, we could initialize the pet instance with a vaccine attribute set to an empty array. We could then append the vaccine to that array when the vaccinations method is called on the pet object. We would then be able to view and update a pet’s vaccines by using dot notation.

Pet Class summary

In summary, we can think of objects in our program as representations of real-life objects. A class is a template that outlines the characteristics and behaviors of an object. The characteristics of an object are it’s attributes and the functions that define the behavior of an object are it’s methods.

Advantages of OOP

Flexibility and Organization

In OOP, an application is designed in modular components, with classes grouping together data and functions that dictate an object’s characteristics and behaviors. This structure helps organize code into manageable pieces of data which can be beneficial when troubleshooting and getting new programmers up to speed on an application’s fundamental purpose.

Security

By leveraging abstraction and encapsulation, programmers can enhance the security of an application. Utilizing abstraction allows them to hide the intricate details of how a program works and only expose the information users need to interact with that application. Encapsulation keeps all of the information related to an object — it’s attributes/properties/methods in one place, providing greater control and restrictions on how an object’s variables can be modified.

Code Reusability

Through inheritance and polymorphism, developers can create new classes based on existing ones and have the ability to modify the expression of methods inherited from other classes. This ability enhances code reusability and reduces the amount of repetition to replicate similar behaviors amongst objects.

In conclusion, Object-Oriented Programming (OOP) serves as a powerful paradigm for software design, offering a structured approach to modeling real-world objects and their interactions. With key principles such as abstraction, encapsulation, inheritance, and polymorphism, OOP empowers developers to create modular, organized, and secure code while promoting code reusability. As OOP continues to play a pivotal role in modern programming, mastering its concepts and practices is essential for building robust and maintainable software solutions.

Sources:

https://www.coursera.org/articles/object-oriented-programming-languages

https://www.statista.com/statistics/793628/worldwide-developer-survey-most-used-languages/

https://www.cs.ucf.edu/~leavens/ComS541Fall97/hw-pages/paradigms/major.html

https://www.interviewbit.com/blog/features-of-oops/

https://herovired.com/learning-hub/blogs/abstraction-in-python/

http://worrydream.com/EarlyHistoryOfSmalltalk/

https://realpython.com/inheritance-composition-python/

https://realpython.com/python-metaclasses/