About me

I am a full stack .net developer with 5 years of experience in front-end and back-end technologies like HTML, CSS, Jquery, Angular, Typescript, Bootstrap, C#, MSSQL and Mongodb. I have done bachelor's in computer engineering from Gujarat Technological University. I have worked on web applications, Web APIs, Windows Forms, Web Forms, Tray applications and corporate websites. Programming is my hobby and I have starting writing blog to provide a platform for those who want to grow their career as a fullstack .NET developers.

Click here to know more about me

Introduction to Object Oriented Programming

What Is Object-Oriented Programming?

Object-Oriented Programming is a model/way of programming based on the concept of classes and objects

What Is the Benefit of Object-Oriented Programming?

With the help of Object-Oriented Programming, it becomes easier to simulate real-world problems and the complex problem can be divided into small reusable chunks that reduce redundancy in programming.

EXAMPLE

Consider a program that prints a list of software developers. First, let us see the procedural way of implementing this code.

PROCEDURAL PROGRAMMING METHOD

function DisplayDetails (name, lang, project) {
  document.write("<li>");
  document.write("<strong>Name:</strong> " + name + "<br/>");
  document.write("<strong>Programming Language:</strong> " + lang + "<br/>");
  document.write("<strong>Current Project:</strong> " + project + "<br/>");
  document.write("</li>");
}

document.write("<ul>");
DisplayDetails("Soumya","c#","Porject1");
DisplayDetails("Soum1","Java","Porject2");
DisplayDetails("Soum2","Python","Porject3");
document.write("</ul>");

This same program can be implemented using Object Oriented Programming Concept

OBJECT ORIENTED PROGRAMMING METHOD

If you are not aware of creating classes and objects in JavaScript, Please visit the article: Classes and Constructors in JS before going further with this article

class softwaredeveloper {
  constructor(name, lang, proj) {
    this.Name = name;
    this.Language = lang;
    this.Project = proj;
  }
  DisplayDetails() {
    document.write("<li>");
    document.write("<strong>Name:</strong> " + this.Name + "<br/>");
    document.write("<strong>Programming Language:</strong> " + this.Language + "<br/>");
    document.write("<strong>Current Project:</strong> " + this.Project + "<br/>");
    document.write("</li>");
  }
}

let dev1 = new softwaredeveloper("Soumya","c#","Porject1"); 
let dev2 = new softwaredeveloper("Soum1","Java","Porject2"); 
let dev3 = new softwaredeveloper("Soum2","Python","Porject3"); 
document.write("<ul>");
dev1.DisplayDetails();
dev2.DisplayDetails();
dev3.DisplayDetails();
document.write("</ul>");

WHAT IS THE ADVANTAGE OF USING OOP?

In the above example, if there is a need to diplay only names of the progrmmers, instead of creating a function for that and passing the paramters again, we can make a method that displays the object's name as shown in the below snippets:

PROCEDURAL PROGRAMMING METHOD

function DisplayDetails(name, lang, project) {
  document.write("<li>");
  document.write("<strong>Name:</strong> " + name + "<br/>");
  document.write("<strong>Programming Language:</strong> " + lang + "<br/>");
  document.write("<strong>Current Project:</strong> " + project + "<br/>");
  document.write("</li>");
}

function DisplayNames(name) {
  document.write("<li>");
  document.write("<strong>Name:</strong> " + name + "<br/>");
  document.write("</li>");
}


document.write("<ul>");
DisplayDetails("Soumya","c#","Porject1");
DisplayDetails("Soum1","Java","Porject2");
DisplayDetails("Soum2","Python","Porject3");
document.write("</ul>");

document.write("<ul>");
DisplayNames("Soumya");
DisplayNames("Soum1");
DisplayNames("Soum2");
document.write("</ul>");

OBJECT ORIENTED PROGRAMMING METHOD

  class softwaredeveloper {
  constructor(name, lang, proj) {
    this.Name = name;
    this.Language = lang;
    this.Project = proj;
  }
  DisplayDetails() {
    document.write("<li>");
    document.write("<strong>Name:</strong> " + this.Name + "<br />");
    document.write("<strong>Programming Language:</strong> " + this.Language + "<br />");
    document.write("<strong>Current Project:</strong> " + this.Project + "<br />");
    document.write("</li>");
  }
   DisplayNames() {
    document.write("<li>");
    document.write("<strong>Name:</strong> " + this.Name + "<br />");
    document.write("</li>");
  }
}

let dev1 = new softwaredeveloper("Soumya","c#","Porject1"); 
let dev2 = new softwaredeveloper("Soum1","Java","Porject2"); 
let dev3 = new softwaredeveloper("Soum2","Python","Porject3"); 
document.write("<ul>");
dev1.DisplayDetails();
dev2.DisplayDetails();
dev3.DisplayDetails();
document.write("</ul>");

document.write("<ul>");
dev1.DisplayNames();
dev2.DisplayNames();
dev3.DisplayNames();
document.write("</ul>");

Below are the JSFiddle Links for the above examples:
Procedural Code Example
OOP Example

This way the real-time problems can be simulated using Object-Oriented Programming. Object-Oriented Programming has four principles. They are:

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

I am gonna cover these concepts in further articles. I hope this article is useful to understand what OOP is. For any queries, you can write to me at info@beawesomewithprogramming.co.in. To get notified for the releases, subscribe through email. If you found this article useful, please share it. Thank you 😊.