4 Core Concepts of Object Oriented Programming

Olivia
3 min readFeb 10, 2021
Object-oriented programming

At the moment everyone’s favourite question for me seems to be:

‘Do you know what object oriented programming is?’

The first time I was asked, I didn’t know. The second time I was asked, I knew slightly, but that wasn’t good enough for me. The next person to ask me will receive a fully detailed answer, an essay even. So buckle up for my official answer…

someone: What is Object Oriented Programming?

me: What a great question! Object Oriented Programming (OOP) is a programming paradigm that makes developing large projects easier and has four core concepts:

  • Encapsulation
  • Inheritance
  • Abstraction
  • Polymorphism

OOP differs a little per language, for example in Ruby, C#, JavaScript and Python, but the overall concept is the same.

Encapsulation

OOP centres around ‘objects’, common examples are to imagine a cat, a dog or a person. Each of these objects will have their own individual attributes and their own individual methods/behaviours like walk, talk, meow, bark, and so on. To stop these objects from interacting with each other when they shouldn’t, they need to be encapsulated.

Encapsulation refers to holding each object inside a defined boundary, this limiting interference from other objects. A bonus of this is data hiding and security measures.

Class names are a way of encapsulating and producing objects that are alike. Cats and dogs can be stored under the class of ‘Animal’, but not a person. If you had a class called ‘Language’ you could produce objects called Ruby, Python and JavaScript!

In Ruby, if we wanted to create a class like the above, it would look like this:

class Language def initialize(name, creator)    @name = name    @creator = creator end
def description
puts "I'm #{@name} and I was created by #{@creator}!" endendruby = Language.new("Ruby", "Yukihiro Matsumoto")python = Language.new("Python", "Guido van Rossum")javascript = Language.new("JavaScript", "Brendan Eich")
ruby.descriptionpython.descriptionjavascript.description

What we’ve done here is defined the class name, and give it two variables (name, creator) unique to that class and its objects. Whatever object we produce from this class will use those variables.

The def is a method (function) that each object we create under Language will have access to.

The lines referring to Language.new()are filling out the parameters for name & creator, for example “Python” was created by “Guido van Rossum” — there’s your fun fact for the day. So when we call javascript.description we are calling the method defined above, and we will get an output of

“I’m JavaScript and I was created by Brendan Eich!”

instead of “I’m #{@name} and I was created by #{@creator}!”.

Inheritance

Now we move on to inheritance, which is when the classes essentially share (inherit) from each other. A vague example of this is a class called Computers and class called Phones could inherit from each other for a method call send_email, rather than having to type it all out again for every object you have that can send an email.

Abstraction

Abstraction is a bit like a cover blanket. It hides what isn’t relevant for the use of other objects and only exposes high methods that are public. When you turn your phone on, it’s done at the click of a button, and a click of a button is all we see on the surface, but inside your phone much more than that happened underneath the hood, especially for mobiles that have finger-ID. Abstraction hides all the details and technical pieces. This is also a security measure against exposing personal data.

Polymorphism

Polymorphism is what it sounds like. Morphing, different shapes, different forms. Objects can ‘morph’ when necessary and take on many forms in a parent — child relationship. There are two types of polymorphism: static and dynamic. But that’s an explanation for part two.

Happy coding!!!

--

--

Olivia

From Law Graduate to Fullstack Developer / Professional Googler. Join me as I blog about the highs & lows of learning to code and navigating my new career path