What is object oriented programming? 

  • Reading time:15 mins read

Object oriented or object-oriented? 

You can see the phrase written in many ways, but the grammatically correct way to write it is with a hyphen: object-oriented.

We can also find the name of this programming paradigm as an acronym so we are going to use that as well: OOP.

 

what is object oriented programming

What is object oriented programming: a programming paradigm

Object-oriented programming is one of the most important programming paradigms. It is a system of principles that define what basic logic we use to solve a given task and to structure our programs, and how elements of the code are connected to each other.

A programming language can follow many paradigms, and a piece of code can contain many paradigms as well. There are procedural, object-oriented, functional and logical programming paradigms.

Advantages of object-oriented programming:

  • It is easier to model complex things as simpler, more reproducible structures
  • The code is easily reusable
  • We can create programs that run faster with it
  • The structure of the code is more transparent
  • Development is faster because parallel classes can be developed at the same time
  • The program can be modified, debugged, and maintained easier
  • It is safer (encapsulation and abstraction makes data more secure) 

The most popular object-oriented programming languages:

  • C++
  • C#
  • Java
  • JavaScript 
  • Python

Objects vs classes

The main concept of object-oriented programming is the object. Just like real-life objects, objects in programming have properties and methods. 

The structure of data and functions stored in objects does not have to be re-entered over and over again when programming. To make the coding process and the code simpler and more economical, we create specific blueprints called classes. These contain the basic information about the structure of objects, and the objects contain more specific pieces of data and methods.

  • We create a “cars” class that has every property and method we need. The name of the brand, the colour of the car, and the date of manufacture will be stored in the object that can be “run” (method).
  • We assign the structure and logic of the “cars” class to each new car. We can create a “1988 red Polski” or a “2010 silver BMW” this way.
  • Every time we enter a new car into the system, we can reuse the code that is contained in our “cars” class.

A small dictionary of object-oriented programming

Let’s look at a few common terms through the lens of object-oriented programming:

What is it?  What information does it contain? Operations Example
class General “blueprint” or draf attributes behaviour (defined with methods) cars
object Specific properties state, data methods Polski, BMW

How are object-oriented programs structured?

Let’s take a look at a real-life example: imagine that you are the manager of a dog boarding house that has more than 100 dog guests a year, and you want to know who is currently in the house, how long they will be there, and other basic information.

The objects of the two dogs (Bucky and Sparky) would look something like this in JavaScript:

 

As you can see, there are many repeating elements in the two objects, for example, the age() function. If we gather up the repeating pieces of code and group them together in a class, the code becomes shorter and more manageable.

How to think like a programmer when you need to create the database for the dog boarding house?

  1. You need to create a parent class for every property and method that needs to appear for every dog. This will be the blueprint for your objects.
  2. You need to create child classes that represent subcategories you need to use.
  3. You need to add the unique properties and behaviours that differentiate the dogs in each subcategory.
  4. You need to create objects for each dog from the child classes.

So you need to classify the shared properties and methods to create a simple blueprint, then you need to create subclasses that will define the dogs’ specific behaviour.

The 4 basic principles of object-oriented programming

Object-oriented programming builds on 4 basic principles:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

We are going to look at this one by one, with examples.

Encapsulation

  • Every property and method of an object is safely stored within the object as a unit. The methods of other objects can only affect them if we let them. Every object can have public and private variables and methods. 
  • Public variables can be accessed and used by other objects, while private variables cannot. Encapsulation makes our code more secure.

Let’s see a few examples of encapsulation

  • In our previous cars example: the colour, the year, and the name of the model are private variables, other objects cannot change them.
  • Starting the car could be a public method: other objects, like “person”, for example, can call it.
  • New example: roleplaying game: 
    • A hero can have typically private variables like name, clothes, hair colour, and methods like attack, run and jump.
    • They can also have public properties like “health points” and methods like “attack”, that other objects can have access to and even modify. The “enemy” object, for example, can call this function and modify the value of the “health points” variable of our hero when it attacks.
    • In our dog boarding house example, this means that the dog owners can only access the data of their own dogs. This is how it looks in JavaScript:

 

Abstraction

  • The essence of abstraction is that it provides only the necessary information to the outside world and hides the internal details of execution. 
  • Programs often consist of units made up of thousands of lines of code and are hard to manage. Abstraction helps maintain the code and makes it more transparent for the user.
  • Through abstraction, objects only show elements that are important in the surface operation and interaction with other objects, but they hide the internal structure.
    • In the “cars” example: the “start” method contains the piece of code that describes what is happening when we call this function when we “start” the car. The object that is called it (e.g., the “person” object that starts the car) doesn’t need to know exactly what code is running in the background, only that the “car” is working, so it starts after we call the “start” method.
    • In our roleplaying game: the “attack” method probably contains many lines of code that describes how the attack works, and what effects it will have on our hero. The “enemy” object does not need to know how the attack works, it just needs to know that the value of the hero’s “health points” variable will decrease.

Inheritance

  • There are many difficulties with long, complex pieces of code: they are not only hard to manage and look through but there can also be many similar snippets of code or many similar objects. They can have the same logic, longer pieces of code can be the same in them, but the objects are not full copies of each other.
  • In order to not create new objects for everything, we can use inheritance. We use the terms “parent” and “child” in programming: children inherit from their parents.
  • During inheritance, the child object inherits the parent object’s logic and structure. The child classes can automatically access the functions of the parent.
  • Inheritance supports simple reusability.

 

  • Let’s look at our cars example again: some of the “cars” object’s variables (colour, name of the model, date of manufacture) can be inherited by a new object, meaning we can create similar objects that have some different properties as well. For example “truck”, “van”, and “tractor”.
  • We can find many examples of inheritance in our roleplaying game as well. There are probably many different types of enemies in the game (like “orc”, “dragon”, “elf”, etc.), and these can all be children of the “enemy” parent object. They can inherit many properties from the original object (“move”, “attack”), but they can have different clothes and weapons.
  • In our dog boarding house example, we can see inheritance at work when we created subcategories (subclasses) within the class of dogs, for herding dogs and rescue dogs. They both inherit the properties of the parent class and the “bark” method.

 

object-oriented programming

 

Polymorphism 

  • Polymorphism lets child objects inherit the properties of their parents while still having their own methods which are used in different ways. So children can implement their parents’ functions in different ways.
    • If the “cars”, “truck”, and “tractor” objects are all descendants of the “vehicle” class, they can inherit its properties. Through polymorphism, the “start” method can run differently in each child object.
    • In our game, all enemies (like the “orc” and “dragon” objects) inherit the “attack” method from the “enemy” object, but their “attack” is implemented in different ways.
    • With our dogs, we can set up the herding dogs (child class) to have a different bark, to implement the “bark” method differently than the parent class. For example, in the console, these dogs will say “Woof!” instead of “Bark!”. The child class overwrites the method of the parent class.

Object-oriented programming is a complex concept. The examples in this article might seem simple, but they are usually used at an advanced level. However, there are elements and aspects that we can implement at a beginner level as well. For example, we can create and use simple objects in JavaScript, Java, and Python. 

Thanks to educative.io

In this article, you read about the basics of object-oriented programming and learned about the 4 main principles. In writing our article, we relied heavily on educative.io’s article on object-oriented programming and its amazing figures. Thank you for creating a useful and understandable article on this topic.

 

object oriented programming

 

In CodeBerry Programming School’s “Basics of Programming” series, we discuss programming-related topics and everything you need to know to get started. Stay tuned for new articles and find the answer to every “what”, “why”, and “how”, you may have had about coding!

Basics of Programming

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.