What Is OOP?: Example
What Is OOP?: Example
3. Merits of OOP
1. Reusability: Code can be reused across projects by leveraging
classes and inheritance.
Example: A "Vehicle" class can be reused in applications
involving cars, bikes, or planes.
2. Modularity: Programs are divided into smaller, manageable
parts (classes).
3. Scalability: Easy to add new features or modify existing code
without affecting other parts.
4. Maintainability: Encapsulation ensures better code
organization and reduces complexity.
5. Real-World Modeling: Closely mirrors real-world entities and
their interactions.
6. Abstraction: Simplifies complex problems by focusing on high-
level design.
7. Data Security: Encapsulation restricts unauthorized access to
sensitive data.
4. Demerits of OOP
1. Learning Curve: Can be challenging for beginners to grasp
concepts like inheritance and polymorphism.
2. Overhead: OOP programs may be slower due to abstraction
layers and dynamic method resolution.
3. Complexity: Overuse of objects and inheritance can lead to
code bloat and confusion.
4. Resource Intensive: Object creation and management can
consume more memory compared to procedural programming.
Summary
Object-Oriented Programming is a powerful paradigm for modern
software development, offering benefits like reusability, scalability,
and real-world modeling. While it has a steeper learning curve and
resource overhead, its advantages make it a preferred choice for
many applications. Examples of OOP languages include Java, Python,
and C++, each suited for various domains.
Concepts of OOP
1. Reusability
Reusability means using existing code (like classes and methods) in
new programs without rewriting it. It saves time and effort.
Example:
Suppose you create a class Vehicle with attributes like speed and fuel.
You can reuse this class for different vehicles like Car or Bike
without creating everything from scratch.
java
Copy code
class Vehicle {
int speed;
int fuel;
void displayInfo() {
System.out.println("Speed: " + speed + " Fuel: " + fuel);
}
}
2. Encapsulation
Encapsulation means wrapping data (attributes) and methods
(behaviors) together in a single unit (class) and restricting direct
access to some parts of the data.
How it works: Use private for attributes and provide public
methods (getters and setters) to access them.
Why? To protect sensitive information and maintain control.
Example:
java
Copy code
class BankAccount {
private double balance; // Cannot be accessed directly
4. Abstraction
Abstraction hides unnecessary details and shows only the essential
features. This helps simplify complex systems.
How? Use abstract classes or interfaces.
Why? To focus on "what" a class does rather than "how" it does
it.
Example:
java
Copy code
abstract class Animal {
abstract void sound(); // Abstract method, no implementation
}
5. Inheritance
Inheritance allows a class (child) to inherit properties and methods
from another class (parent). This enables code reusability and the
creation of specialized classes.
Example:
java
Copy code
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
6. Composition
Composition is when a class is made up of objects of other classes.
It’s a "has-a" relationship.
Example:
A Car is composed of an Engine and Wheels.
java
Copy code
class Engine {
void start() {
System.out.println("Engine starts");
}
}
class Car {
Engine engine = new Engine(); // Composition
void drive() {
engine.start();
System.out.println("Car is moving");
}
}
7. Aggregation
Aggregation is a specialized form of composition where one class
uses another but doesn’t own it. It’s a "has-a" relationship but
weaker than composition.
Example:
A Team has Players, but players can belong to multiple teams.
java
Copy code
class Player {
String name;
Player(String name) {
this.name = name;
}
}
class Team {
String teamName;
Player[] players;
8. Overriding
Overriding allows a subclass to provide its own implementation of a
method from the parent class.
Why? To change or enhance the behavior of a parent class
method.
Example:
java
class Animal {
void sound() {
System.out.println("Some sound...");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark!");
}
}
9. Polymorphism
Polymorphism allows objects to take many forms. A method can
behave differently depending on the object calling it.
Types: Method Overloading and Method Overriding.
Why? To handle different cases with a single interface.
Example:
java
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}