DEV Community

Cover image for Stay in the L(OOP) with Object-Oriented Programming Basics
Jordan Burroughs
Jordan Burroughs

Posted on

Stay in the L(OOP) with Object-Oriented Programming Basics

"Object-oriented programming is an exceptionally bad idea which could only have originated in California."
-Edsger Dijkstra

Whether you love it or hate it object-orientated programming (OOP) is one of the dominant models in the field. In college, the meaning and concepts of OOP were constantly drilled into my mind. Even in some job interviews I was expected to know these fundamentals off the top of the head. That's why I think it's important to discuss them now.

Presenting these concepts will allow me to introduce a newbie to crucial high level knowledge, and provide a quick refresher to anyone who is already aware of these ideas (and even help myself achieve a deeper degree of understanding).

In this blog I will briefly describe OOP and broadly explain its 4 main concepts: abstraction, encapsulation, inheritance, and polymorphism. I come from a Java background so I will sprinkle some Java in to expand on some points.

Note: My aim is for the following explanations to be general. Rather than showing the same examples and code that everyone uses, I'm going to present from a different perspective.๐Ÿ˜

Spongebob OOP

What is Object-Oriented Programming?

OOP is a computer programming model that organizes software design through the use of objects and classes.

Objects are the basic units of OOP. They can emulate real-world entities, like a dog, table, or car. That means they can hold data and perform behaviors.

An object's data is implemented using fields, and behaviors are implemented using methods.

Classes are simply blueprints for an object. They define the state and behavior that the objects can invoke.

Objects are instances of classes

Here is a quick example to show these relationships:
Classes, objects, fields, and methods example

1. Abstraction

The process of abstraction hides unnecessary details from an object and shows only essential attributes. Therefore, a limited amount of information is exposed to the user for interaction.

For example, when driving a car, you don't worry about inner workings of the engine or understanding how the tires steer your car. All of that is behind the scenes.

You simply interact with the basic mechanisms (provided by the interface) to do what the car is supposed to do, like pressing the gas and turning the wheel. You don't care how these operations are carried out.

It is the same in OOP. For example, if you instantiate an Airplane object and invoke its function to prepareForTakeoff(), all you know is that the Airplane will prepare for takeoff. However, in the code, other things could be happening as well, such as testing the wings and running diagnostics on the engines.

Abstraction airplane example

Abstraction is achieved through the use of abstract classes and interfaces in Java.

Abstraction == Implementation Hiding

2. Encapsulation

Encapsulation protects object data by wrapping it in a single unit, allowing for modification and visibility to be controlled. Rather than directly accessing the data, you must invoke a function that modifies or retrieves it for you. This adds a crucial layer of security for the object.

For example, if you have a table object with public fields, you would be able to set the fields any time you want. You may think that it wouldn't matter, but what if you set table.legs = null somewhere accidentally? That could break the entire object and application (because it's no longer a table).

However, if you encapsulate that field in a setter (setLegs(null)), you can add a check that ignores setting "legs" to null, thus hardening the application.

Encapsulation table example

Encapsulation is achieved through the use of access modifiers (i.e. public, private, protected) and getters/setters in Java.

Encapsulation == Information Hiding

3. Inheritance

Inheritance allows one class to derive data and behaviors from another class. The main benefit of this is for reusability, thus saving time and effort while making the code more readable.

For example, a Student class and a Teacher class can be children of a Person class. The Student and Teacher inherit fields (firstName, lastName, dateOfBirth) and methods (doWork(), goToSchool()) from the Person class.

The Student or Teacher classes now have access to the Person data and methods. So any objects that instantiate these two classes can invoke them as well (if they are public).

Inheritance student-teacher example

In Java, inheritance is achieved by "extending" the parent class.

4. Polymorphism

Poly-what?? Honestly, it sounds cooler than it actually is.

Polymorphism is the condition of occurring in several different forms.

In OOP, polymorphism allows data or methods with the same name mean different things in different contexts. But what does that even mean?

Take a look at the inheritance example once again. Since both Student and Teacher inherit doWork() from the Person class, they can both call it. We can also have them implement their own way to "doWork".

  • A Teacher can grade papers
  • A Student can study

Polymorphism student-teacher example

This is known as overriding the method from the parent class. Now whenever a Teacher object is created and the doWork() function is called, the Teacher will "grade papers". And if a Person object was created and calls doWork(), it would just "read a book".

Another case where you will see polymorphism at work is through overloading a method. Overloading is achieved by changing the number of arguments and/or changing the type of arguments in a method.

For example, in a class called House you can have the following methods:

  • paint()
  • paint(String color)
  • paint(String color1, String color2)
  • paint(boolean usePrimer)


The explanations for these four concepts are very general because I wanted to make these as simple as possible. Leave a comment below with any other words of wisdom regarding these topics, or OOP as a whole. Thanks!

If you want a deeper insight on how these ideas are implemented programmatically check out these links from "GeeksforGeeks":

Thanks for reading! If you want more tech tips, software stuff, and bussin' blogs, you can throw me a follow on Twitter๐Ÿ”ฅ๐Ÿค˜๐Ÿฝ๐Ÿถ

Top comments (4)

superails profile image
Yaroslav Shmarov

You did a good job phrasing it in your words! :check

jburroughs profile image
Jordan Burroughs

Thanks! I was really trying to ๐Ÿ˜…

mustabelmo profile image
Mustapha Belmokhtar

This article is awesome <3

jburroughs profile image
Jordan Burroughs