Challenge: Chained Account Constructors
Swipe to start coding
Practice constructor chaining by building an Account class that offers flexibility in how accounts are created. Use overloaded constructors to allow for different initialization scenarios, and ensure all constructors ultimately delegate initialization to a single constructor to avoid code duplication.
- The
Accountclass must have three constructors: a default constructor, a constructor that takes anaccountNumber, and a constructor that takes both anaccountNumberand abalance. - The default constructor must set both
AccountNumberandBalanceto zero. - The constructor with one parameter must set
AccountNumberto the provided value andBalanceto zero. - The constructor with two parameters must set
AccountNumberandBalanceto the provided values. - All constructors must use constructor chaining so that only one constructor contains the actual assignment logic.
- Do not duplicate assignment code across constructors.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain this in simpler terms?
What are the main points I should remember?
Can you give me an example?
Awesome!
Completion rate improved to 4.17
Challenge: Chained Account Constructors
Swipe to show menu
Swipe to start coding
Practice constructor chaining by building an Account class that offers flexibility in how accounts are created. Use overloaded constructors to allow for different initialization scenarios, and ensure all constructors ultimately delegate initialization to a single constructor to avoid code duplication.
- The
Accountclass must have three constructors: a default constructor, a constructor that takes anaccountNumber, and a constructor that takes both anaccountNumberand abalance. - The default constructor must set both
AccountNumberandBalanceto zero. - The constructor with one parameter must set
AccountNumberto the provided value andBalanceto zero. - The constructor with two parameters must set
AccountNumberandBalanceto the provided values. - All constructors must use constructor chaining so that only one constructor contains the actual assignment logic.
- Do not duplicate assignment code across constructors.
Solution
Thanks for your feedback!
single