An Overview on the 4 Main Principles of Object Oriented Programming

Rebecca Govert
6 min readSep 7, 2018

--

Super excited about my journey in “his” world, my husband loves throwing terms at me like “Principles of Object Oriented Programming (OOP)” to help me learn important computer programming concepts. So here is my brief over view OOP and the four principles.

Object Oriented Programming has four main principles: inheritance, polymorphism, abstraction, and encapsulation. Object oriented programming is a paradigm where the programming is organized by objects. In this organization certain objects will be higher in the hierarchy than others, will contain objects inside of themselves, or be encased in other objects. In Ruby every thing is an object and all objects have identities, can hold state, and each object displays behavior by responding to methods(or messages). In JS, “an object is a collection of related data and/or functionality (which usually consists of several variables and functions — which are called properties and methods when they are inside objects)” according to MDN.

Inheritance — the principle that a new object can inherit the attributes of an existing object. A class can inherit attributes from another class. When talking about classes there are base or super classes and derived or sub classes, which can be referred to as parent and child classes. The base/super/parent class is the class from which the derived/sub/child class inherits properties, while the derived/sub/child class also has properties of its own.

One way to understand inheritance is to look at a class in Ruby: as one can see (below), we can think of classes similarly to the animal tree. I have over simplified the example. Cat has attributes from Vertebrate which gets attributes from Animal and Octopus has attributes from Invertebrate which gets attributes from Animal. Cat and Octopus only share attributes from Animal.

   ---- Ruby Simplified Example of Class and Inheritance ----class Animal                 <----- BASE/SUPER/PARENT
attr_accessor :life
def initialize(life)
@life = life
end
end
class vertebrate < Animal <----- DERIVED/SUB/CHILD of ANIMAL
attr_accessor :backbone and SIBLING TO VERTEBRATE
def initialize(backbone) CLASS
@no_backbone = no_backbone
end
end
class invertebrate < Animal <----- DERIVED/SUB/CHILD of ANIMAL
attr_accessor :no_backbone and SIBLING TO VERTEBRATE
def initialize(no_backbone) CLASS
@no_backbone = no_backbone
end
end
class cat < vertebrate <----- DERIVED/SUB/CHILD of VERTEBRATE
attr_accessor :species and ALSO GRANDCHILD of ANIMAL
def initialize(species) (not sibling of octopus)
@species = species
end
end
class octopus < invertebrate <----- DERIVED/SUB/CHILD of VERTEBRATE
attr_accessor :species and ALSO GRANDCHILD of ANIMAL
def initialize(species) (not sibling of octopus)
@species = species
end
end
*** --- Javascript Class syntax difference --- ***class Animal
constructor(life)
life = life
end
end
(--by me)

In Javascript, I kept coming across the statement “Javascript does not have classes”, so I looked into this further. Javascript did not have classes prior to 2015. In fact according to the MDN documentation, “classes in Javascript are primarily syntactical sugar over JavaScript’s existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.” Today, we use classes, even though, a class is not exactly the same as it is in Ruby. The main point with inheritance is to understand that an object (class) can inherit attributes from another object (class) and also have new additional attributes of its own.

Polymorphism — is from Greek meaning to have multiple forms. In programming, polymorphism is the term used for being able to assign different meaning or usage to an object, function , or variable in different contexts and so each can have more than one form. Our variable can change throughout our programming. A real world example of polymporphism is to think of a specific person, in my example I am going to pick myself (Becci). At home Becci is a wife. At school, Becci is a student. At work, Becci is an employee. However, in each one of these class identifications, I am still Becci. The class morphs to fit the desired attributes.

Abstraction — is the concept of hiding all the relevant data about an object in order to 1) reduce complexity and 2) increase efficiency. The idea is that just because all the information is gathered and available, only some of it needs to be pulled (or abstracted to be used in a specific instance. We can look at all the information we hold as individuals in the United States: date of birth, place born, social security number, bank accounts, …. and the list gets long. When filling out a new account for facebook, we would only pull out the necessary data to use on the form and leave the rest hidden. This is a broad sense of abstraction.

Encapsulation — to me goes hand in hand with the abstraction principle. n a simplistic way, encapsulation describes the idea of bundling data and methods that work on that specific data within in one unit(IE: classes), which is then, also used to hide the internal state of an object from the outside or client. The concept is to keep another class from seeing or having access to the methods or functions else where (ie. how they work). This is a behind the scenes concept. While JavaScript classes aren’t exactly the Ruby’s classes, JavaScript classes do allow the features of data hiding. Data hiding is a key component of Encapsulation. To encapsulate is to enclose (something) in or as if in a capsule.

Understanding the difference between encapsulation and abstraction can be difficult. My favorite comparison was an answer Yassif Shaikh gave including the picture(below) to a StockOverflow question. According to Yassif Shaikh:

Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.

Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.

Yasser R Shaikh: StackOoverflow

REVIEW:

  1. Inheritance — allows a class to “inherit” from another class.
  2. Polymorphism — Greek: ‘having multiple forms”: being able to assign a different meaning or usage to an object/function/variable in different contexts and so have more than one form.
  3. Abstraction — hide all the relevant data about an object in order to both reduce complexity and increase efficiency. It is the concept that just because all of the information is gathered, only some of it needs to be pulled (or abstracted) to be used in a specific instance.
  4. Encapsulation — restricting data access by way of using classes and accessing functions and methods. It is keeping the how hidden from the use.

--

--

Rebecca Govert

Junior Software Engineer | Web Developer — Passionate about People and Tech