JavaScript – Abstraction


What is an abstraction?

It is the process of displaying only essential things to the user.

Its main goal is to avoid complexity by hiding unnecessary details from the user.


Abstraction can be explained by abstract class and methods.

This is one of the key concepts in object oriented programming concepts.

Abstraction can be explained by,

1. We cannot create instance of the abstract class

Example :

<script>
			function mainFunction(){
				this.name = name;
				throw new Error("Instance of the abstract class cannot be created");
			}
			mainFunction.prototype.display = function(){
			
			return this.name;
			}
			var a = new mainFunction();
		</script>

Output :

Javascript online course !!!

Click to Learn More about – Javascript online learning

2. Achieving abstraction

Example :

<script>
			function mainFunction(){
				this.name = name;
				throw new Error("Instance of the abstract class cannot be created");
			}
			mainFunction.prototype.show = function(){
				return this.name;
			}
			function subClass(name){
				this.name = name;
			}
			subClass.prototype = Object.create(mainFunction.prototype);
			var a = new subClass("Abdul Kalaam");
			document.write("The Name is <b>" + a.show()+"</b><br/>");
			var b = new subClass("Robert Kunt");
			document.write("The Name is <b>" + b.show()+"</b><br/>");
		</script>

Output :