JavaScript Polymorphism

Javascript is based on the OOPs Concept, i.e., Javascript is an object-oriented programming language that 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>

Sub-class and parent class have the 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>