JavaScript Tutorial

Javascript OOPS Concept

JS Encapsulation

Encapsulation in JavaScript involves binding data (variables) with the functions that operate on that data. It enables us to control and validate the data. To achieve encapsulation in JavaScript, consider the following guidelines:

  • Use the var keyword to declare data members as private.
  • Utilize setter methods to set the data and getter methods to retrieve the data

Encapsulation enables us to interact with an object using the following properties:

  • Read/Write: Use setter methods to write data and getter methods to read the data.
  • Read Only: Use getter methods exclusively.
  • Write Only: Use setter methods exclusively.

Input:-

   

Output:-

JS Inheritance

Inheritance in JavaScript is a mechanism that allows us to create new classes based on existing classes. It provides the ability for a child class to reuse methods and variables from a parent class.

To create a child class based on a parent class, we use the 'extends' keyword in JavaScript. This allows the child class to inherit all the properties and behaviors of its parent class. Here are some important points to remember when using inheritance:

It maintains an IS-A relationship.

  • Inheritance establishes an IS-A relationship between classes.
  • The 'extends' keyword is used in class expressions or class declarations to indicate inheritance.
  • By using the 'extends' keyword, we can inherit properties and behaviors from both built-in objects and custom classes.
  • In addition to class-based inheritance, JavaScript also supports a prototype-based approach for achieving inheritance.

Inheritance is a powerful feature in JavaScript that promotes code reusability and allows for the creation of hierarchical class structures.

Input:-

   

Output:-

JS Polymorphism

Polymorphism is a fundamental concept in object-oriented programming that allows a single method to be called on different objects, resulting in different behaviors. In JavaScript, polymorphism enables us to use the same method name to perform different actions based on the specific object being referenced.

By defining a method with the same name in different classes or objects, we can achieve polymorphic behavior. When the method is invoked, the JavaScript runtime determines the appropriate implementation to execute based on the object's type or class.

Polymorphism promotes code flexibility, reusability, and extensibility. It allows us to write more generic code that can work with various object types, making our programs more adaptable and scalable.

Input:-

   

Output:-

JS Abstraction

Abstraction is a concept in programming that allows us to hide the internal complexities and focus on the essential features and behaviors of an object or system. In JavaScript, abstraction is often achieved through the use of abstract classes or interfaces.

An abstract class is a blueprint for other classes and cannot be instantiated itself. It defines common properties and methods that derived classes can implement or override. By defining abstract methods, an abstract class specifies the behavior that must be implemented by its subclasses.

Abstraction helps in reducing code duplication by providing a common interface and defining the essential functionality that derived classes must adhere to. It allows for modular and organized code, making it easier to maintain and extend applications.

By utilizing abstraction, developers can focus on the high-level design and functionality of their code, while hiding the underlying implementation details. This promotes code reusability, and flexibility, and improves the overall structure and readability of the program.

Input:-

   

Output:-


Go back to Previous Course