OOPs java
OOPs java
Concepts in Java
Real-Time Examples to Understand Core Concepts
BY:
S Surya Jayasree
What is OOP?
• Definition: Object-Oriented Programming is a programming paradigm
based on the concept of "objects," which can contain data in the form
of fields (attributes) and methods (functions).
Core Concepts:
• Encapsulation,
• Abstraction,
• Inheritance, and
• Polymorphism.
Benefits of OOP
• Modularity: Code is organized into classes and objects.
• Reusability: Classes can be reused in other programs.
• Maintainability: Easier to maintain and modify code.
• Scalability: Can handle large, complex systems better.
Encapsulation (with Example)
Definition: Encapsulation is the concept of hiding the internal details of an object and
providing access through public methods (getters and setters).
class Car {
private String brand;
private int speed;
// Getter
public String getBrand() {
return brand;
}
// Setter
public void setBrand(String brand) {
this.brand = brand;
}
}
Real-Time Example: A car’s engine (private) is hidden; you interact with it using the car’s
dashboard controls (public methods).
Abstraction (with Example)
Definition: Abstraction is the process of hiding complex implementation details and
showing only the essential features.
Example:
abstract class Animal {
abstract void sound(); // Abstract method
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Real-Time Example: A TV remote control hides the complexities of the TV’s inner
workings but lets you control the TV’s functions (like turning it on/off or changing the
channel).
Inheritance (with Example)
Definition: Inheritance is a mechanism where a new class inherits properties and behavior
(methods) from an existing class.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
Real-Time Example: A child inherits traits (height, hair color) from their parents.
Polymorphism (with Example)
Definition: Polymorphism allows objects of different classes to be treated as objects of a common
superclass. It also allows methods to behave differently based on the object type.
Example:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Meow");
}
}
Real-Time Example: The "make sound" command can trigger different responses (e.g., bark or meow)
based on the animal.
Real-Time Example - Banking System
Scenario: Let's build a simple banking class Account {
system using OOP concepts: private double balance;
public void deposit(double amount)
Class: Account (Encapsulation: account {
balance is private) balance += amount;
Inheritance: SavingsAccount extends }
Account public void withdraw(double
amount) {
Polymorphism: Different types of accounts
balance -= amount;
calculate interest differently.
}
Abstraction: Methods like withdraw(), }
deposit() are abstracted from the user. class SavingsAccount extends
Account {
// Interest calculation logic
}
Conclusion
• Recap: OOP in Java provides a powerful approach to organizing and
managing code with concepts like Encapsulation, Abstraction,
Inheritance, and Polymorphism.
• Benefits: Makes code modular, easier to maintain, and reusable.
• Final Thought: OOP principles can be applied across various domains,
from gaming to banking systems, to create scalable, efficient
solutions.