Javascript OOPS Concept

JS Encapsulation

It is a process of binding the data (i.e. variables) with the functions acting on that data and it allows us to control the data and validate it. To achieve an encapsulation in JavaScript, following things to keep in mind: –

  • Use var keyword to make data members private.
  • Use setter methods to set the data and getter methods to get that data.

The encapsulation allows us to handle an object using the following properties:-

  • Read/Write – use setter methods to write the data and getter methods read that data.
  • Read Only – use getter methods only.
  • Write Only – use setter methods only.
Input:- 
    <script>
       class Employee  {  
            constructor() {  
                var name, marks;  
            }  
            getName() {  
                return this.name;  
            }  
            setName(name) {  
                this.name=name;  
            }  
            getMarks() {  
                return this.marks;  
            }  
            setMarks(marks) {  
                this.marks=marks;  
            }  
        }  
        var emp = new Employee();  
        emp.setName("Mike");  
        emp.setMarks(40);  
        document.writeln(emp.getName()+" "+emp.getMarks());	   </script>  

Output:-


JS Inheritance

It is a mechanism that allows us to create new classes on the basis of already existing classes. It also provides flexibility to the child class to reuse the methods and variables of a parent class.

The JavaScript ‘extends’ keyword is used to create a child class on the basis of a parent class and it facilitates child class to acquire all the properties and behavior of its parent class. Following points need to remember while using inheritance :-

  • It maintains an IS-A relationship.
  • The extends keyword is used in class expressions or class declarations.
  • Using extends keyword, we can acquire all the properties and behavior of the inbuilt object as well as custom classes.
  • We can also use a prototype-based approach to achieve inheritance
Input:- 
    <script>
       class Moment extends Date {  
            constructor() {  
                super();  
            }}  
        var m = new Moment();  
        document.write(m.getDate()+"-"+(m.getMonth()+1)+"-"+m.getFullYear());	   
	   </script> 

Output:-


JS Polymorphism

It is a core concept of an object-oriented paradigm that provides a way to perform a single action in different forms. It provides an ability to call the same method on different JavaScript objects.

Input:- 
    <script>
       class A {  
            display() {  
                document.write("A is called");  
            }  
        }  
        class B extends A { }  
        var b = new B();  
        b.display();      
	   </script> 

Output:-


JS Abstraction

An abstraction is a way of hiding the implementation details and showing only the functionality to the users. We can also say that it ignores the irrelevant details and shows only the required one.

We cannot create an instance of Abstract Class and reduces the duplication of code.

Input:- 
    <script>
       function Vehicle() {  
            this.vehicleName="vehicleName";  
            throw new Error("We cannot create an instance of Abstract Class");  
        }  
        Vehicle.prototype.display=function() {  
            return "Vehicle: "+ this.vehicleName;  
        }  
        function Bike(vehicleName) {  
            this.vehicleName = vehicleName;  
        }  
        Bike.prototype=Object.create(Vehicle.prototype);  
        var bike=new Bike("Honda");  
        document.write(bike.display());
	   </script>

Output:-