(HandWritten Notes)
https://drive.google.com/file/d/18mldgbyPhkf1BfqvwKSYSM9WGcZsYaG5/view?usp=drive_link
four main pillars of Object-Oriented Programming (OOP) in simple terms:
1.Encapsulation:
- What it is: Encapsulation is like putting data and the methods (functions) that operate on that data inside a single unit, which is called a class. This keeps the data safe from outside interference and misuse.
- Why it’s useful: It allows you to control how the data is accessed and modified. You can make certain data private and only accessible through methods, which provides security and avoids unwanted changes.
- Example: Imagine you have a class
Car
. It contains data likespeed
and methods likedrive()
andstop()
. You can make thespeed
private so it can only be changed by using a specific method likesetSpeed()
. This way, you can control how the speed is modified.
class Car {
private int speed; // private variable
public void setSpeed(int speed) {
if (speed > 0) {
this.speed = speed; // control how speed is set
}
}
public int getSpeed() {
return speed; // getter method to access speed
}
}
Here, speed
is encapsulated (hidden) inside the Car
class and can only be accessed through setSpeed()
and getSpeed()
methods. This is encapsulation.
2. Inheritance:
- What it is: Inheritance is when one class takes on the properties and methods of another class. The class that gives its properties is called the parent (or base class), and the class that inherits is called the child (or derived class).
- Why it’s useful: It helps in code re-usability. If multiple classes share similar features, you can put the shared features in a parent class and the child classes can reuse them. Plus, child classes can also add their own features.
- Example: Let’s say we have a
Vehicle
class with common properties likespeed
and methods likedrive()
. We can create specific classes likeCar
andBike
that inherit these properties fromVehicle
.
class Vehicle {
int speed;
public void drive() {
System.out.println("Vehicle is driving");
}
}
class Car extends Vehicle {
public void honk() {
System.out.println("Car is honking");
}
}
In this example, the Car
class inherits the drive()
method from Vehicle
. This means Car
objects can use both drive()
and their own honk()
method.
3. Polymorphism:
What it is: Polymorphism allows one name to have many forms. In OOP, this means you can use the same method or function name to perform different tasks, based on the context.
Why it’s useful: It makes code flexible and reusable. A method can behave differently based on the input or the object that calls it. There are two main types:
- Compile-time Polymorphism (Method Overloading): When you have multiple methods with the same name but different parameters.
- Run-time Polymorphism (Method Overriding): When a child class has a method with the same name as one in the parent class but gives it a new implementation.
Example of Method Overloading (Compile-time Polymorphism):
class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
Here, the add()
method is overloaded: one version takes two integers, and the other takes two doubles.
Example of Method Overriding (Run-time Polymorphism):
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
In this case, Dog
overrides the sound()
method of Animal
. Even though the method name is the same, the behavior is different.
4. Abstraction:
- What it is: Abstraction means showing only the essential details and hiding the complexity. You focus on what an object does, not how it does it.
- Why it’s useful: It helps reduce complexity by allowing you to focus on the high-level functioning of your program without worrying about the details.
- Example: You know how to drive a car (you use methods like
accelerate()
andbrake()
), but you don’t need to know exactly how the engine works.
In Java, abstraction is usually implemented using:
- Abstract classes: Classes that can have both defined methods (with code) and undefined methods (without code).
- Interfaces: They contain only method signatures, and any class that implements the interface must provide its own implementation for those methods.
- Example of Abstraction with an Interface:
interface Animal {
void sound(); // method signature without implementation
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
Here, Animal
is an interface that defines what a sound method should look like. The Dog
class implements this interface and provides its own behavior.
Quick Recap:
- Encapsulation: Hides data and gives controlled access.
- Inheritance: Allows a class to use the properties and methods of another class.
- Polymorphism: One name, many forms (method overloading and overriding).
- Abstraction: Hides unnecessary details and shows only the essential parts.