Sitemap

Object Oriented Programming ES6

2 min readMay 2, 2020

--

Object Oriented Programming: Simply, Object Oriented Programming means object-based programming. A lot of objects use here to make programming easier, reusable and easy to manage. This is a programming style, not a tool. This style involves breaking a program into segments of objects that can communicate with each other.

Declaring Class In OOP ES6:

// class keyword is required to create a new classclass ClassName {constructor() {//initializing class properties}//class methods defined}

Create Object Instance from class: This easy to create a object instance from a class.

class employee{// Construct function is required to declare propertiesconstructor(name,age,designation){//all properties defined as they were in the constructor functionthis.name = namethis.age = agethis.designation = designationthis.displayName = function() {console.log(“Name is:”,this.name)}}}//creating an object instance named “employeeObj”var employeeObj = new employee(‘Joe’,22,’Developer’)//displaying the properties of employeeObjemployeeObj.displayName()console.log(“Age is”,employeeObj.age)console.log(“Designation is:”,employeeObj.designation)

3 Main Concepts of OOP: Three core concepts of OOP are:

  • Inheritance
  • Polymorphism
  • Encapsulation

Inheritance: Inheritance allows us to create a new class by inheriting or extending an existing class. With this feature, we can access properties and methods from the super ( existing class ) class. We can add new property and method based on our requirement. Example:

// Super classclass Vehicle {constructor(driver, wheels){this.driver = driver;this.wheels = wheels;}}// Child Class.class Bus {constructor(driver, wheels, seats){super(driver, wheels); // using super function we can access properties from parent classthis.seats = seats; // This is new property}}

Above the example, we can make a lot of objects according to our needs.

Get Shrikanta Mazumder’s stories in your inbox

Join Medium for free to get updates from this writer.

Polymorphism: Polymorphism means the ability of an object to appear in different forms. It refers to a programming language to process data differently depending on their data-type or class.

Encapsulation: Encapsulation is the bundling of data and the methods. These data are restricted to access from outside the bundle. That means object stores its state privately and only the objects method has access to change it.

Static Methods: The static keyword defines a static method for a class. Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.

Ex:

class ClassName {static staticMethod () {return ‘I am from static method’;}}console.log(ClassName.staticMethod());// Result: I am from static methodconst class1 = new ClassName();console.log(class1.staticMethod());// This will return error.

--

--