Encapsulation
class Area {
// Fields to calculate area
int length;
int breadth;
// Constructor to initialize values
Area(int length, int breadth) {
this.length = length;
this.breadth = breadth;
}
// Method to calculate area
public void getArea() {
int area = length * breadth;
System.out.println("Area: " + area);
}
}
class Main {
public static void main(String[] args) {
// Create object of Area, pass values of length and breadth
Area rectangle = new Area(5, 6);
rectangle.getArea();
}
}Last updated