Skip to content

javaadde/oops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Object-Oriented Programming (OOP) Concepts in JavaScript

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.

📚 Table of Contents

🎯 The Four Pillars of OOP

Abstraction

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.

Encapsulation

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.

Inheritance

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.

Polymorphism

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:

  1. Compile-time Polymorphism (Static): Method overloading
  2. Runtime Polymorphism (Dynamic): Method overriding

Example: Different animals (Dog, Cat) making different sounds when the same speak() method is called.

🚀 Getting Started

Prerequisites

  • Node.js (version 12 or higher)
  • Basic understanding of JavaScript

Running the Examples

  1. Clone or download this repository
  2. Navigate to the project directory
  3. Run any of the example files using Node.js:
node abstraction.js
node encapsulation.js
node inheritance.js
node polymorphism.js

Expected Output

Each 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

📁 File Structure

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

🎓 Learning Objectives

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

💡 Key Takeaways

  1. Abstraction simplifies complex systems by hiding unnecessary details
  2. Encapsulation protects data integrity and provides controlled access
  3. Inheritance promotes code reusability and logical organization
  4. Polymorphism enables flexible and extensible code design

🔧 Modern JavaScript Features Used

  • ES6 Classes
  • Private fields (# syntax)
  • Method overriding
  • Constructor functions
  • Arrow functions (where applicable)

📖 Additional Resources


This repository serves as a practical guide to understanding Object-Oriented Programming concepts in JavaScript. Each example is designed to be runnable and educational.

About

object oriented programming concepts and examples of it

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published