0% found this document useful (0 votes)
8 views7 pages

Object

Object-Oriented Programming (OOP) in JavaScript organizes code using objects that contain data and actions, emphasizing encapsulation, inheritance, polymorphism, and abstraction. A class serves as a blueprint for creating objects, while methods are functions defined within classes. Key differences between methods and functions include their definitions and how they are called, with methods being object-specific and functions being standalone.

Uploaded by

nailamubarak351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Object

Object-Oriented Programming (OOP) in JavaScript organizes code using objects that contain data and actions, emphasizing encapsulation, inheritance, polymorphism, and abstraction. A class serves as a blueprint for creating objects, while methods are functions defined within classes. Key differences between methods and functions include their definitions and how they are called, with methods being object-specific and functions being standalone.

Uploaded by

nailamubarak351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

OOP in JavaScript (Simple Explanation)

What is OOP?

OOP (Object-Oriented Programming) is a way to organize your code using "objects". Each object
contains:

 Data (called properties/attributes)

 Actions (called methods/functions)

OOP helps you model real-world things in your code and follows 4 main ideas:

1. Encapsulation: Keeping data and actions together

2. Inheritance: Sharing features between objects

3. Polymorphism: The Same action works differently for different objects

4. Abstraction: Hiding complex details

Class

A class is a template or blueprint used to create objects with the same properties and
behaviors. It defines:

 What data the object will have (properties)

 What actions can it do (methods)

Example: A "Car" class might have properties like color and speed, and methods
like drive() and brake().

Object

An object is a real thing created from a class. Those objects will have some state (properties)
and behavior (methods).

Example: From our "Car" class, we can make actual car objects like myCar with color:
"red" and speed: 0.
4 Pillars of OOP (Simple Explanation)

1. Encapsulation (Data Hiding)

 Keeps data (variables) and methods (functions) together in a class.

 Protects data by making it private, allowing access only through methods.

Example:

2. Inheritance (Code Reuse)

 A child class gets properties & methods from a parent class.

 Avoids rewriting the same code.

Example:
3. Polymorphism (Same Method, Different Behavior)

 The same method works differently in different classes.

Example:

4. Abstraction (Hide Complexity)

 Shows only essential features and hides unnecessary details.


Example:

Summary:

 Encapsulation: Hide data, expose only methods.

 Inheritance: Reuse code from the parent class.

 Polymorphism: Same method, different actions.

 Abstraction: Show only what’s necessary.

These make OOP clean, reusable, and easy to manage! 🚀

Method vs Function – Key Differences


In programming, both methods and functions are blocks of reusable code, but they have key
differences based on where they are defined and how they are called.

1. What is a Function?

A function is a standalone block of code that performs a task.

 Defined outside of any object/class.

 Called directly by its name.

 The Function is independent.

2. What is a Method?

A method is a function that belongs to an object or class.

 Defined inside an object/class.

 Called using an object reference (obj.method()).

 The Method is dependent.

Example:
When to Use Which?

✔ Use a Function when the code is not tied to any object.


✔ Use a Method when the code operates on object data (this.name, this.age, etc.).

Summary

 Function → Standalone, called directly (func()).

 Method → Belongs to an object, called via object (obj.method()).

 This behaves differently in functions vs methods.

🚀 Remember:

 All methods are functions, but not all functions are methods!

 Methods are object-specific, while functions are general-purpose.

Key Takeaways
✔ Encapsulation: Uses private/# to hide data.
✔ Inheritance: Uses extends & super for parent-child relationships.
✔ Polymorphism: Uses method overriding (same name, different logic).
✔ Abstraction: Uses abstract/interface to hide complexity.

You might also like