Javascript Practice Day 3 :Abstract class and inheritance
Abstract class in Javascript cannot be instantiated with new keyword and therefore any object that needs to be created will be done by Object.creat() method
// Abstract Class - any attempt to create an instance will throw an error
var shape = function(){
this.shapeName= none
throw new Error(" Cannot create instance of an abstract class");
}
//declaring a method in the abstract class
shape.prototype.draw=function(){
return "Drawing "+this.shapeName;
}
// a class circle which inherits the draw method from the abstract class shape
function circle(shapeName)
{
this.shapeName=shapeName;
}
//the instance of the object class is created by the Object.create method and assigned to circle class
circle.prototype=Object.create(shape.prototype);
// new instance of circle class is created
var c = new circle("circle");
document.write("Circle object created <br/>");
the draw method of shape class is called via the circle class object
document.write(c.draw() +"<br/>");
function Square (shapeName)
{
this.shapeName=shapeName;
}
Square.prototype=Object.create(shape.prototype);
var s = new Square("Square");
document.write("Square object created <br/>");
document.write(s.draw() +"<br/>");
// Abstract Class - any attempt to create an instance will throw an error
var shape = function(){
this.shapeName= none
throw new Error(" Cannot create instance of an abstract class");
}
//declaring a method in the abstract class
shape.prototype.draw=function(){
return "Drawing "+this.shapeName;
}
// a class circle which inherits the draw method from the abstract class shape
function circle(shapeName)
{
this.shapeName=shapeName;
}
//the instance of the object class is created by the Object.create method and assigned to circle class
circle.prototype=Object.create(shape.prototype);
// new instance of circle class is created
var c = new circle("circle");
document.write("Circle object created <br/>");
the draw method of shape class is called via the circle class object
document.write(c.draw() +"<br/>");
function Square (shapeName)
{
this.shapeName=shapeName;
}
Square.prototype=Object.create(shape.prototype);
var s = new Square("Square");
document.write("Square object created <br/>");
document.write(s.draw() +"<br/>");
Comments
Post a Comment