Write a program that uses the this keyword to call one constructor from another
This Java code defines a class called Rectangle. In this class, rectangles are represented by their length and width attributes. There are two constructors provided:
In the main method, an instance of Rectangle is created using the constructor that takes a single parameter, which creates a square with sides of length 7. When this instance is created, the length and width of the square are printed, showing that both are 7.
class Rectangle { int length, width; Rectangle(int side) { this(side, side); } Rectangle(int l, int w) { length = l; width = w; System.out.println("Length : " + length); System.out.println("Width : " + width); } public static void main(String[] args) { Rectangle square = new Rectangle(7); } }
Length : 7 Width : 7
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions