Example to illustrate Inheritance using JavaScript

In the example we define a base object called Car, and Create sub object called ferrari that inherits attributes from Car.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaScript - Inheritance Example</title>
</head>
 
<body>
<p>
  <script language="JavaScript" type="text/JavaScript">
 
 
 // ************************************************************************
 //Define a Car object
 // ************************************************************************
 function Car()
 {
	 //Add attributes to the Car
	 this.mileage = 0;
	 this.category = "Vehicle";
 }
 
 
  // ************************************************************************
 // Define a ferrari object
 // ************************************************************************
 function ferrari()
 {
 	 //Add attributes to the ferrari
 	this.mileage = 100;
	this.weight = 400;
 }
 
 // ************************************************************************
 // Use the ferrari prototype to inherit from Car
  // ************************************************************************
 ferrari.prototype = new Car();
 
  //Use the ferrari prototype to inherit from Car
 ferrari.prototype.getMileage = function()
 {
    return this.mileage;
 }
 
 // ************************************************************************
  // Use the ferrari prototype to access Car attributes
  // ************************************************************************
 CashRegister.prototype.getCategory = function()
 {
    return this.category;
 }
 
 // ************************************************************************
 // Define a JavaScript function to drive the test
 // ************************************************************************
 function performTestonInheritance()
 {
 
	  // ************************************************************************
	  // Create an instance of myFerrari
	  // ************************************************************************
	  var myFerrari= new ferrari();
 
	 // ************************************************************************
	 // Access the redifined attribute of ferrari
	 // ************************************************************************
	 alert("The mileage is: " + myFerrari.getMileage() );
 
	 // ************************************************************************
	 // Access the base attribute of ferrari
	 // ************************************************************************
	 alert("The category is: " + myFerrari.getCategory() );
 
 }
 
</script>
 
 
</p>
Inheritance in Javascript  Example
<br/>
<form name="form1" method="post" action="">
  <div align="center">
 
    <!-- Add button to call JavaScript function -->
    <input type="button" name="peformTest" value="Perform Inheritance Test" onclick="javascript:performTestonInheritance()">
 
  </div>
</form>
</body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.