FullStack Extra9 OOPS
FullStack Extra9 OOPS
In-Depth Theory
Object-Oriented Programming (OOP) is a programming paradigm centered around objects and classes.
1. Encapsulation:
- Bundling data and methods inside a class
- Restrict direct access using private/protected modifiers
2. Abstraction:
- Hide internal implementation and show only relevant features
- Achieved using abstract classes and interfaces
3. Inheritance:
- Enables one class (child) to inherit properties and behaviors from another (parent)
- Promotes reusability
4. Polymorphism:
- Ability to take many forms
- Overloading: same method name, different parameters
- Overriding: child class redefines parent method
5. Class vs Object:
- Class: blueprint for creating objects
- Object: instance of a class
6. Constructor:
- Special method used to initialize objects
- Can be overloaded
7. Access Modifiers:
- public, private, protected (C++, Java, C#)
- __ (Python for private by convention)
8. OOP in Practice:
- Python: everything is an object
- Java: strict class-based OOP
- C++: supports both procedural and object-oriented
- JavaScript: prototype-based OOP
Code Examples
1. Python:
class Animal:
def __init__(self, name): self.name = name
def speak(self): print("Sound")
class Dog(Animal):
def speak(self): print("Bark")
2. Java:
class Shape {
void draw() { System.out.println("Draw Shape"); }
}
class Circle extends Shape {
void draw() { System.out.println("Draw Circle"); }
}
3. C++:
class Vehicle {
public:
void honk() { cout << "Beep!"; }
};
Car c;
c.honk();
4. JavaScript:
class User {
constructor(name) { this.name = name; }
greet() { console.log("Hi " + this.name); }
}
Practice Assignment
1. Python: Create an abstract class Shape with area() method. Derive Rectangle and Circle.
2. Java: Create a class Employee with name, id and salary. Add constructor and getter methods.
5. Bonus:
- Implement multiple inheritance using mixins (in Python or JavaScript)
- Add access control using private/protected modifiers where possible