Introduction to Object Oriented Programming in Javascript

es6

Object Oriented Programming is programming technique involving thinking of software in terms of Objects that interact with each other.

Normally several concepts get associated with this paradigm:

  1. Encapsulation.
  2. Polymorphism.
  3. Inheritance.
  4. Aggregation.

Also terms such as Class,Object, Method and Property.

Encapsulation and Information Hiding

Encapsulation specifies that Objects contain:

  1. Data via properties.
  2. Action/Behavior via methods.

The data that is stored inside the Objects can be hidden from other objects.

Yet those objects may still be able to perform actions involving those objects. They do these via methods.

However, the implementation details of those methods as well as the data they are working on are hidden from the outside world.

In some languages like Java and C#, accessibility modifiers are used such as private,protected and public to specify the visibility level.

However in Javascript all methods and properties are inherently public and other mechanisms are used to protect data.

Inheritance

Inheritance allows us introduce Parent-Child relationships to objects. This is a kin to the real world where there always a hierarchical relationship.

Classes are the blueprints for creation of objects. And these classes can inherit from other classes in Object Oriented Programming.

The child inherits methods and properties from parent class.

The child can re-define the inheritted method. We call this method overriding.

Aggregation

Aggregation is also known as composition. It basically involves combining several objects into a new one.

Aggregation allows breaking a problem into smaller and easier to handle parts. A technique generally called divide and conquer.

Aggregation, by it allowing us to break problems into smaller chunks, brings about a higher level of abstraction.

Polymorphism

Polymorphism is the ability of calling the same method on different objects, with each of them responding in their own way.

Once you've inherited from a base class, for example, then you can call a method defined in that base class via the base class reference or the child class reference.

And each of those calls may mean different tasks being executed.

Objects

Objects are the basis of Object Oriented Programming.

Object Oriented Programming is normally considered powerful yet easier than other paradigms because with objects we can model the real world or concepts.

This then provides with a mental framework for thinking through program design.

The characteristics of that object can then be representated via properties while the actions that the object can take by methods.

Objects are always formed from classes.

Classes

A class is a template for object creation. I can create a Person class for instance then from it create Mike,Matilda and an arbitray number of John Does.

We said at the introduction that Objects do contain data and behavior. Well those data and behavior are defined in the class.

Then the instance of that class, which is the object, can obtain those access the properties and those methods which define the data and behavior respcetively.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post