011-003-012
Calling Another Constructor with this()
Easy
Problem Description
Calling Another Constructor with this()
In this problem: You will create a program where a Rectangle class uses this() to call another constructor, avoiding code duplication.
Learning Objective: Understand the basics of calling another constructor within the same class using this()
Overview
When defining multiple constructors in the same class, this() is used to avoid duplicating common initialization logic. The basic pattern is calling a parameterized constructor from a no-argument constructor.
Specifications
Rectangleclass:width(int),height(int) fieldsRectangle(): default constructor, initializes with this(1, 1)Rectangle(int width, int height): full-argument constructorgetArea()method returns the area- Create two Rectangle instances in Main and print information
Output Format
Default: 1 x 1, Area: 1
Custom: 5 x 3, Area: 15
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up