Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Constructor overloading
📖 Scenario: You are creating a simple program to represent a Book in a library system. Sometimes you know all details about the book, and sometimes you only know the title.
🎯 Goal: Build a Book class with two constructors: one that takes only the title, and another that takes title, author, and year. Then create two Book objects using these constructors and print their details.
📋 What You'll Learn
Create a class called Book with three fields: title, author, and year
Add a constructor that takes only string title and sets author to "Unknown" and year to 0
Add a constructor that takes string title, string author, and int year and sets all fields
Create two Book objects: one using the single-parameter constructor and one using the three-parameter constructor
Print the details of both books in the format: Title: {title}, Author: {author}, Year: {year}
💡 Why This Matters
🌍 Real World
Constructor overloading helps create flexible classes that can be initialized with different sets of information, like creating user profiles with varying details.
💼 Career
Understanding constructor overloading is important for software developers to write clean, reusable, and flexible code in object-oriented programming.
Progress0 / 4 steps
1
Create the Book class with fields
Create a class called Book with three public fields: string title, string author, and int year.
C Sharp (C#)
Hint
Use public string title; to declare the fields inside the class.
2
Add two constructors to the Book class
Add two constructors inside the Book class: one that takes string title and sets author to "Unknown" and year to 0, and another that takes string title, string author, and int year and sets all fields.
C Sharp (C#)
Hint
Use this.title = title; inside each constructor to set the fields.
3
Create two Book objects using both constructors
In the Main method, create two Book objects: book1 using the constructor with only title "C# Basics", and book2 using the constructor with title "Advanced C#", author "John Doe", and year 2023.
C Sharp (C#)
Hint
Use new Book("C# Basics") to create book1.
4
Print the details of both books
Add Console.WriteLine statements to print the details of book1 and book2 in the format: Title: {title}, Author: {author}, Year: {year}.
C Sharp (C#)
Hint
Use Console.WriteLine($"Title: {book1.title}, Author: {book1.author}, Year: {book1.year}"); to print details.
Practice
(1/5)
1. What does constructor overloading in C# allow you to do?
easy
A. Override methods with the same name
B. Create multiple constructors with different parameter lists in the same class
Hint: Check which constructor is called for each object [OK]
Common Mistakes:
Assuming default int value 0 instead of assigned 5
Confusing which constructor runs for each object
Mixing up output order
4. Identify the error in this constructor overloading code:
class Person {
public string name;
public Person(string n) { name = n; }
public Person(string n) { name = n.ToUpper(); }
}
medium
A. Constructor name does not match class name
B. Missing return type in constructors
C. Duplicate constructor with same parameter list
D. Cannot assign string to name
Solution
Step 1: Check constructor parameter lists
Both constructors have the same parameter type and count (string n), causing duplication.
Step 2: Understand overloading rules
Constructors must differ by parameter types or count to overload; identical signatures cause error.
Final Answer:
Duplicate constructor with same parameter list -> Option C
Quick Check:
Same parameters = duplicate constructor error [OK]
Hint: Constructor signatures must differ by parameters [OK]
Common Mistakes:
Thinking constructors can differ by body only
Adding return type mistakenly
Ignoring parameter list uniqueness
5. You want to create a class Rectangle with overloaded constructors: - One constructor takes no parameters and sets width and height to 1. - Another takes one parameter and sets both width and height to that value. - Another takes two parameters to set width and height separately. Which of these constructor definitions correctly implements this?
hard
A. public Rectangle() { width = 1; height = 1; } public Rectangle(int w, int h) { width = w; height = h; } public Rectangle(int w, int h) { width = w; height = h; }
B. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int size) { width = size; height = size; }
C. public Rectangle(int w, int h) { width = w; height = h; } public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; }
D. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; }
Solution
Step 1: Check parameter lists for uniqueness
public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } has three constructors with distinct parameter lists: no parameters, one int, and two ints.
Step 2: Verify each constructor sets values correctly
Each constructor sets width and height as required: default 1, same size, or separate sizes.
Final Answer:
public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } -> Option D
Quick Check:
Distinct parameter lists and correct assignments [OK]
Hint: Each constructor must have unique parameter count or types [OK]
Common Mistakes:
Defining two constructors with same parameter types
Mixing order of constructors causing confusion
Not setting default values in parameterless constructor