This repository demonstrates the four fundamental pillars of Object-Oriented Programming using JavaScript examples. Each concept is illustrated with practical code examples to help understand how OOP principles work in JavaScript.
File: abstraction.js
Abstraction is the concept of hiding complex implementation details and showing only the essential features of an object. It allows users to interact with objects at a high level without needing to understand the internal workings.
Key Points:
- Hide complex implementation details
- Show only essential features
- Simplify interaction with objects
- Focus on what an object does, not how it does it
Example: A car's interface (steering, brake, accelerator) abstracts away the complex engine mechanics.
File: encapsulation.js
Encapsulation is the practice of bundling data and methods together while restricting direct access to an object's internal state. It provides controlled access through well-defined interfaces.
Key Points:
- Hide internal data from outside world
- Provide controlled access through methods
- Improve security and data integrity
- Use private fields and getter/setter methods
Example: A bank account with private balance that can only be modified through deposit/withdrawal methods.
File: inheritance.js
Inheritance allows a class to inherit properties and methods from another class, creating a parent-child relationship. This promotes code reusability and establishes an "is-a" relationship.
Key Points:
- Create parent-child class relationships
- Reuse code from parent classes
- Establish "is-a" relationships
- Override parent methods when needed
Example: A Dog class inheriting from an Animal class, inheriting the ability to speak but with its own implementation.
File: polymorphism.js
Polymorphism allows objects of different classes to be treated as objects of a common parent class while maintaining their unique behaviors. The same method call can produce different results based on the object type.
Key Points:
- Same interface, different implementations
- Runtime method resolution
- Method overriding
- Code flexibility and extensibility
Types of Polymorphism:
- Compile-time Polymorphism (Static): Method overloading
- Runtime Polymorphism (Dynamic): Method overriding
Example: Different animals (Dog, Cat) making different sounds when the same speak() method is called.
- Node.js (version 12 or higher)
- Basic understanding of JavaScript
- Clone or download this repository
- Navigate to the project directory
- Run any of the example files using Node.js:
node abstraction.js
node encapsulation.js
node inheritance.js
node polymorphism.jsEach file will demonstrate its respective OOP concept with console output showing:
- How the concept works in practice
- Different behaviors based on the concept
- Real-world analogies and examples
OOPS/
├── README.md           # This file - project documentation
├── abstraction.js      # Abstraction examples and concepts
├── encapsulation.js    # Encapsulation with private fields
├── inheritance.js      # Class inheritance examples
└── polymurphism.js     # Polymorphism demonstrations
After studying these examples, you should understand:
- Abstraction: How to hide complexity and provide simple interfaces
- Encapsulation: How to protect data and control access
- Inheritance: How to create class hierarchies and reuse code
- Polymorphism: How to write flexible code that works with multiple object types
- Abstraction simplifies complex systems by hiding unnecessary details
- Encapsulation protects data integrity and provides controlled access
- Inheritance promotes code reusability and logical organization
- Polymorphism enables flexible and extensible code design
- ES6 Classes
- Private fields (#syntax)
- Method overriding
- Constructor functions
- Arrow functions (where applicable)
This repository serves as a practical guide to understanding Object-Oriented Programming concepts in JavaScript. Each example is designed to be runnable and educational.