011-002-012
Constructor Overloading: Person Class
Medium
Problem Description
Constructor Overloading: Person Class
In this problem: You will implement a Person class with three overloaded constructors that accept different arguments, create instances using each constructor, and display person information.
Learning Objective: Understand constructor overloading and learn initialization patterns with different arguments
Overview
Define three constructors in the Person class:
Person(): Sets name to "Unknown" and age to 0Person(String name): Sets the given name, age defaults to 0Person(String name, int age): Sets both name and age
Specifications
- Define
name(String) andage(int) fields inPersonclass display()method outputs "Name: [name], Age: [age]"- In
Mainclass, create instances using each constructor and call display()
Output Format
Name: Unknown, Age: 0
Name: Alice, Age: 0
Name: Bob, Age: 25
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