CodesJava

Easy learning with example program codes

JavaScript Polymorphism

Javascript is based on OOPs Concept, i.e, Javascript is an object-oriented programming language which uses classes and objects for computations.

Polymorphism is the OOPs principle which provides the facility to perform one task in many ways.

Sub class object calls the parent class method example:

<!DOCTYPE html>
<html>
<body>
 
<script>
class Welcome
  {
 	show()
	{
  	document.writeln("Hello World!");
	}
  }
class Bye extends Welcome
  {
  }
var bye=new Bye();
bye.show();
</script>
 
</body>
</html>

Output:

Hello World!

Sub class and parent class have same method example:

<!DOCTYPE html>
<html>
<body>
 
<script>
class Welcome
  {
 	show()
	{
  	document.writeln("Welcome class method.");
	}
  }
class Bye extends Welcome
  {
   show()
	{
  	document.writeln("Bye class method.");
	}
  }
var welcome=new Welcome();
welcome.show();
 
var bye=new Bye();
bye.show();
</script>
 
</body>
</html>

Output:

Welcome class method. Bye class method.
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status