A capsule in which main medicine is encapsulated.
A car is in which engine, wheels, and other parts are encapsulated.
class Car
{
// Data members (variables)
String brand;
int speed;
// Method to display person details
void setDetails(String b, int s)
{
brand = b;
speed = s;
}
void printDetails()
{
System.out.println("Brand : " + brand);
System.out.println("Speed : " + speed);
}
}
public class Main
{
public static void main(String[] args)
{
// Creating object
Car c = new Car();
// Calling method
c.setDetails("Tata", 100);
}
}
Brand : Tata Speed : 100
Person
class contains both data (name
, age
) and method (displayInfo()
).
class Car
{
// Private data members (encapsulated)
private String brand;
private int speed;
// Public setter for brand
public void setBrand(String brand)
{
this.brand = brand;
}
// Public getter for brand
public String getBrand()
{
return brand;
}
// Public setter for speed
public void setSpeed(int speed)
{
// Optional: simple validation
if (speed >= 0)
{
this.speed = speed;
}
}
// Public getter for speed
public int getSpeed()
{
return speed;
}
// Method to print car details
public void printDetails()
{
System.out.println("Brand : " + brand);
System.out.println("Speed : " + speed);
}
}
public class MainApp
{
public static void main(String[] args)
{
Car c = new Car();
// Setting values using setters
c.setBrand("Tata");
c.setSpeed(100);
// Printing car details
c.printDetails();
}
}
Speed : Tata Speed : 100
// Class demonstrating proper encapsulation
class Account
{
// π Protects data by hiding it from direct access
private String accountHolder;
private double balance;
// β
Public getter (controlled access to private data)
public String getAccountHolder()
{
return accountHolder;
}
// β
Public setter (controlled access with flexibility for future validation)
public void setAccountHolder(String accountHolder)
{
this.accountHolder = accountHolder;
}
// β
Getter for balance
public double getBalance()
{
return balance;
}
// π° Method to deposit money
public void deposit(double amount)
{
// π‘οΈ Allows data validation before modifying balance
if (amount > 0)
{
balance = balance + amount;
System.out.println("You have deposited " + amount + " Rs.");
System.out.println("New balance is: " + getBalance() + " Rs.");
}
else
{
System.out.println("Invalid deposit amount");
}
}
// πΈ Method to withdraw money
public void withdraw(double amount)
{
// π‘οΈ Data validation: prevents negative balance
if (amount > 0 && amount <= balance)
{
balance = balance - amount;
System.out.println("You have withdrawn " + amount + " Rs.");
System.out.println("New balance is: " + getBalance() + " Rs.");
}
else
{
System.out.println("Invalid or Insufficient balance for withdrawal");
}
}
}
public class BankApp
{
public static void main(String[] args)
{
// π¨βπΌ Creating object
Account account = new Account();
// π« Cannot access private fields directly
// account.balance = 10000; // β Not allowed (Encapsulation)
// β
Uses public setters and methods
account.setAccountHolder("Deepak");
// β
Proper access via methods ensures validation
account.deposit(10000); // Valid deposit
account.withdraw(3000); // Valid withdrawal
account.deposit(-20000); // Invalid deposit
account.withdraw(100000); // Invalid withdrawal (insufficient funds)
}
}
You have deposited 10000.0 Rs. New balance is: 10000.0 Rs. You have withdrawn 3000.0 Rs. New balance is: 7000.0 Rs. Invalid deposit amount Invalid or Insufficient balance for withdrawal
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.