Javascript-OOPs(Part3)-Inheritance - (9)

  

👉Javascript with OOPs (Inheritance)


In Javascript, Inheritance is done using prototype object. A prototype object is an object pointing to another object.


We don't have a direct inheritance in Javascript, we can mimic inheritance using prototype.


Let's say, we have one more class that inherits from Customer, and it submits to Server2.

function CustomerChild(){

        this.submit = function() {

        alert ("this submits the data to Server2" + " "     +  this.CustomerName + " "                                       + this.CustomerCode"); 

}}         

 

CustomerChild.prototype = new Customer();


var cust = new Customer();

cust.CustomerName ("John");

cust.Submit();  --this is a call to the submit method of the Customer class. 


cust = new CustomerChild();

cust.CustomerName ("John");

cust.Submit();  --this is a call to the submit method of the CustomerChild class. 


We don't have the "Protected" keyword in Javascript. So while implementing Inheritance, we have to create public methods and properties in the parent class.


In the above example of the child class, when you make a call to the CustomerName, the js engine first tries to search CustomerName in the CustomerChild class, if it cannot locate it, then it makes a search in the parent class i.e. Customer. While in the case of the "Submit" method, since it already exists in the child class, the js engine considers the "Submit" method of the child class and doesn't look for the same in the parent class.

Comments

Popular posts from this blog

Javascript-OOPs(Part2)-AbstractionAndEncapsulation- (8)

Javascript-OOPs(Part4)-Polymorphism- (10)

Javascript-OOPs(Part1)-Objects - (7)