Object Oriented Programming (OOP) is a programming paradigm that organizes code into objects and classes.
OOP helps developers:
- Reuse code
- Improve maintainability
- Achieve modularity
- Implement real-world entities
- Reduce code duplication
JavaScript supports Object Oriented Programming using:
- Class
- Constructor
- Object
- Inheritance
- Exception Handling
A Class is a blueprint used to create objects.
Syntax:
class ClassName {
}Example:
class Details {
}A constructor is a special method used to initialize object properties.
Syntax:
constructor(parameters){
}The constructor executes automatically whenever an object is created.
An Object is an instance of a class.
Syntax:
var obj = new ClassName();Example:
var student = new Details();<!DOCTYPE html>
<html>
<head>
<title>Object Oriented Programming in JavaScript</title>
</head>
<body>
<p id="cls"></p>
<script>
class Details {
constructor(fname,lname) {
this.fname = fname;
this.lname = lname;
}
gender() {
return "Male";
}
}
var mydetails = new Details("Sachin","Tendulkar");
var r = mydetails.gender();
document.getElementById("cls").innerHTML =
mydetails.fname + " " +
mydetails.lname + " " +
r;
console.log(mydetails);
</script>
</body>
</html>class DetailsCreates a class named:
Details
constructor(fname,lname)Receives:
- fname
- lname
and stores them using:
this.fname = fname;
this.lname = lname;gender(){
return "Male";
}Returns:
Male
var mydetails =
new Details("Sachin","Tendulkar");Creates:
Object of Details Class
mydetails.gender();Output:
Male
Sachin Tendulkar Male
Inheritance means:
Acquiring properties and methods of one class into another class.
JavaScript uses:
extendskeyword.
<!DOCTYPE html>
<html>
<head>
<title>Inheritance in JavaScript</title>
</head>
<body>
<script>
class Parent {
constructor(drink){
this.normalWater = drink;
}
display(){
return "I will have normal " + this.normalWater;
}
}
class Child extends Parent {
constructor(drink,food){
super(drink);
this.eat = food;
}
outfun(){
return this.display()
+ " I will eat "
+ this.eat;
}
}
var fam = new Child("Water","Bread");
console.log(fam.outfun());
</script>
</body>
</html>class ParentContains:
- constructor()
- display()
class Child extends ParentThe Child class inherits:
- normalWater property
- display() method
from Parent class.
super(drink);Calls parent class constructor.
var fam =
new Child("Water","Bread");Creates Child object.
I will have normal Water I will eat Bread
Wrapping variables and methods together inside a class.
Example:
class Student {
constructor(name){
this.name=name;
}
}Acquiring properties of parent class.
Example:
class Child extends ParentSame method behaves differently.
Example:
class Animal{
sound(){
console.log("Animal Sound");
}
}Hiding internal implementation details.
Users only use methods without knowing internal logic.
Exception handling is used to handle runtime errors.
JavaScript uses:
try
catch
finally
try{
// risky code
}
catch(e){
// handle exception
}
finally{
// always executed
}<!DOCTYPE html>
<html>
<head>
<title>Exception Handling</title>
<script>
function fun(){
var str="Sachin";
try{
var low=str.toLowerCase();
alert(low);
}
catch(e){
alert("Exception is handled");
}
finally{
alert("Finally block is executed");
}
}
</script>
</head>
<body>
<h1>Exception Handling</h1>
<input type="button"
value="Click"
onclick="fun()">
</body>
</html>try{
var low=str.toLowerCase();
}Executes risky code.
catch(e)Handles exception if error occurs.
finally{
alert("Finally block is executed");
}Always executes.
Whether:
- Error occurs OR
- Error does not occur
First Alert:
sachin
Second Alert:
Finally block is executed
| Class | Object |
|---|---|
| Blueprint | Instance |
| Logical entity | Physical entity |
| No memory allocated | Memory allocated |
| Used to create objects | Created from class |
| Constructor | Method |
|---|---|
| Special function | Normal function |
| Executes automatically | Called manually |
| Initializes object | Performs operation |
| Only one constructor | Multiple methods allowed |
Answer:
Object Oriented Programming is a programming paradigm that organizes code into classes and objects.
Answer:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Answer:
A class is a blueprint used to create objects.
Answer:
An object is an instance of a class.
Example:
var obj = new Student();Answer:
extendsExample:
class Child extends ParentAnswer:
super() calls the constructor of the parent class.
Answer:
Exception handling is a mechanism used to handle runtime errors gracefully using:
try
catch
finally
Answer:
Yes.
The finally block always executes whether an exception occurs or not.
- JavaScript supports OOP.
- Class is a blueprint.
- Object is an instance of a class.
- Constructor initializes objects.
- extends is used for inheritance.
- super() calls parent constructor.
- try handles risky code.
- catch handles exceptions.
- finally always executes.
- Classes were introduced in ES6 (2015).
Object Oriented Programming in JavaScript provides a structured way to organize code using classes and objects. JavaScript supports constructors, inheritance, encapsulation, and exception handling, enabling developers to build reusable, modular, and maintainable applications. Modern JavaScript uses ES6 classes, making OOP syntax simpler and easier to understand while still relying on JavaScript's prototype-based inheritance internally.