...
/Solution Review: Implement the Rectangle Class Using the Concepts of Encapsulation
Solution Review: Implement the Rectangle Class Using the Concepts of Encapsulation
This review provides a detailed analysis to solve the 'Implement the Rectangle Class using the Concepts of Encapsulation' challenge.
We'll cover the following...
Solution
Press + to interact
// Class Rectangleclass Rectangle {// Private Fieldsprivate int length;private int width;// Default Constructorpublic Rectangle() {this.length = 0;this.width = 0;}// Parameterized Constructorpublic Rectangle(int length, int width) {this.length = length;this.width = width;}// Method to calculate Area of a rectanglepublic int getArea() {return this.length * this.width;}}class Demo {public static void main(String args[]) {Rectangle obj = new Rectangle(2, 2);System.out.println(obj.getArea());}}
Explanation
The solution is straightforward. ...