polymorphism in java
by gowtham[ Edit ] 2010-02-15 10:21:48
function Animal(name) {
this.name = name;
};
Animal.prototype.talk = function() {
return "";
};
function Cat(name) {
Animal.call(this, name);
};
Cat.prototype = new Animal();
Cat.prototype.talk = function() {
return "Meow!";
};
function Dog(name) {
Animal.call(this, name);
};
Dog.prototype = new Animal();
Dog.prototype.talk = function() {
return "Arf! Arf!";
};
var animals = [
new Cat("Missy"),
new Cat("Mr. Mistoffelees"),
new Dog("Lassie")
];
// prints the following:
//
// Missy: Meow!
// Mr. Mistoffelees: Meow!
// Lassie: Arf! Arf!
for (var i=0; i
document.write(animals[i].name + ": " + animals[i].talk() + "");
}