Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  45] [ 6]  / answers: 1 / hits: 115633  / 10 Years ago, wed, december 24, 2014, 12:00:00

I have read some possible article I could found on the internet on polymorphism. But I think I could not quite grasp the meaning of it and its importance. Most of the articles don't say why it is important and how I can achieve polymorphic behavior in OOP (of course in JavaScript).



I can not provide any code example because I haven't got the idea how to implement it, so my questions are below:




  1. What is it?

  2. Why we need it ?

  3. How it works?

  4. How can I achieve this polymorphic behavior in javascript?



I have got this example. But it is easily understandable what will be outcome of this code. It doesn't give any clear idea about polymorphism itself.



function Person(age, weight) {
this.age = age;
this.weight = weight;
this.getInfo = function() {
return I am + this.age + years old +
and weighs + this.weight + kilo.;
}
}
function Employee(age, weight, salary) {
this.salary = salary;
this.age = age;
this.weight = weight;
this.getInfo = function() {
return I am + this.age + years old +
and weighs + this.weight + kilo +
and earns + this.salary + dollar.;
}
}

Employee.prototype = new Person();
Employee.prototype.constructor = Employee;
// The argument, 'obj', can be of any kind
// which method, getInfo(), to be executed depend on the object
// that 'obj' refer to.

function showInfo(obj) {
document.write(obj.getInfo() + <br>);
}

var person = new Person(50,90);
var employee = new Employee(43,80,50000);
showInfo(person);
showInfo(employee);

More From » oop

 Answers
115

Polymorphism is one of the tenets of Object Oriented Programming (OOP). It is the practice of designing objects to share behaviors and to be able to override shared behaviors with specific ones. Polymorphism takes advantage of inheritance in order to make this happen.



In OOP everything is considered to be modeled as an object. This abstraction can be taken all the way down to nuts and bolts for a car, or as broad as simply a car type with a year, make, and model.



To have a polymorphic car scenario there would be the base car type, and then there would subclasses which would inherit from car and provide their own behaviors on top of the basic behaviors a car would have. For example, a subclass could be TowTruck which would still have a year make and model, but might also have some extra behaviors and properties which could be as basic as a flag for IsTowing to as complicated as the specifics of the lift.



Getting back to the example of people and employees, all employees are people, but all people are not employees. Which is to say that people will be the super class, and employee the sub class. People may have ages and weights, but they do not have salaries. Employees are people so they will inherently have an age and weight, but also because they are employees they will have a salary.



So in order to facilitate this, we will first write out the super class (Person)



function Person(age,weight){
this.age = age;
this.weight = weight;
}


And we will give Person the ability to share their information



Person.prototype.getInfo = function(){
return I am + this.age + years old +
and weighs + this.weight + kilo.;
};


Next we wish to have a subclass of Person, Employee



function Employee(age,weight,salary){
this.age = age;
this.weight = weight;
this.salary = salary;
}
Employee.prototype = new Person();


And we will override the behavior of getInfo by defining one which is more fitting to an Employee



Employee.prototype.getInfo = function(){
return I am + this.age + years old +
and weighs + this.weight + kilo +
and earns + this.salary + dollar.;
};


These can be used similar to your original code use



var person = new Person(50,90);
var employee = new Employee(43,80,50000);

console.log(person.getInfo());
console.log(employee.getInfo());


However, there isn't much gained using inheritance here as Employee's constructor is so similar to person's, and the only function in the prototype is being overridden. The power in polymorphic design is to share behaviors.


[#68388] Monday, December 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;