Object Oriented Programming, It is not a rocket science.

Azizi Yusuph
4 min readApr 15, 2022
Photo by Markus Spiske from Pexels

As a new programmer you may have been come across with the term object oriented programming in your daily activities. It is one of the programming styles that include the following concepts;

  • Objects, methods and property
  • Class
  • Encapsulation
  • Aggregation
  • Reusability/inheritance
  • Polymorphism

Objects

An object is the representation of something. Example a cow.

The characteristics of objects example color, name, weight is called properties and its actions example run, sleep, eat, are called methods.

Classes

A class is the recipe for an object. In JavaScript there is no class everything is object, instead we use prototypes

A class is just like a template while an object is a concrete instances based on the templates.

Encapsulation

Photo by Pixabay from Pexels

This is another concept that is being used in regard to object oriented programming; it refers to the information hiding imagine an object, say, an MP3 player. You, as the user of the object, are given some Interface to work with, such as buttons, display, and so on. You use the interface in order to get the object to do something useful for you, like play a song. How exactly the device is working on the inside, you don’t know, and, most often don’t care. In other words, the implementation of the interface is hidden from you. The same thing happens in OOP when your code uses an object by calling its methods. It doesn’t matter if you coded the object yourself or it came from some third-party library; your code doesn’t need to know how the methods work internally. In compiled languages, you can’t actually read the code that makes an object work. In JavaScript, because it’s an interpreted language, you can see the source code, but the concept is still the same–you work with the object’s interface without worrying about its implementation.

Aggregation.

This is another term that is being used in object oriented programming. It means combining several objects into new one. It is the great method of solving problem by separating a problem into smaller manageable parts. Example a house object contain a kitchen object, a toilet object a door object and so on.

Inheritance

Inheritance is an elegant way to reuse existing code. For example, you can have a generic Object, Person, which has properties such as name and date_of_birth, and which also Implements the walk, talk, sleep, and eat functionality. Then, you figure out that you need another object called Programmer. You can re implement all the methods and Properties that a Person object has, but it will be smarter to just say that the Programmer Object inherits a Person object, and saves you some work. The Programmer object only needs to implement more specific functionality, such as the writeCode method, while re using all of the Person object’s functionality.

In classical OOP, classes inherit from other classes, but in JavaScript, as there are no classes, objects inherit from other objects. When an object inherits from another object, it usually adds new methods to the inherited ones, thus extending the old object. Often, the following phrases can be used Interchangeably–B inherits from A and B extends A. Also, the object that inherits can pick one or more methods and redefine them, customizing them for its own needs. This way, the interface stays the same and the method name is the same, but when called on the new object, the method behaves differently. This way of redefining how an inherited method works is known as overriding.

Polymorphism

In the preceding example, a Programmer object inherited all of the methods of the parent person object. This means that both objects provide a talk method, among others. Now imagine that somewhere in your code, there’s a variable called Bob, and it just so happens that you don’t know if Bob is a Person object or a Programmer object. You can still call the talk method on the Bob object and the code will work. This ability to call the same method on different objects, and have each of them respond in their own way, is called Polymorphism.

Thank you for reading my article and let us connect on linkedin, twitter, and instagram

--

--

Azizi Yusuph

Technical Writer | Community builder | Full Stack Software Developer