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
Default constructor
π Scenario: Imagine you are creating a simple program to represent a book in a library. Each book has a title and an author. Sometimes, you want to create a book without giving any details first, and then add details later.
π― Goal: You will create a Book class with a default constructor that sets the title and author to default values. Then, you will create an object of this class and print its details.
π What You'll Learn
Create a class called Book
Add a default constructor to Book that sets title to "Unknown Title" and author to "Unknown Author"
Create an object of Book using the default constructor
Print the title and author of the created Book object
π‘ Why This Matters
π Real World
Default constructors are used to create objects with default settings before setting specific details later.
πΌ Career
Understanding constructors is essential for creating and initializing objects in Java, a key skill for software development.
Progress0 / 4 steps
1
Create the Book class with title and author fields
Create a class called Book with two String fields: title and author.
Java
Hint
Fields are variables inside a class. Use String title; and String author; inside the class.
2
Add a default constructor to Book
Add a default constructor to the Book class that sets title to "Unknown Title" and author to "Unknown Author".
Java
Hint
A default constructor has no parameters and sets the fields inside its body.
3
Create a Book object using the default constructor
In a Main class, create a Book object called myBook using the default constructor.
Java
Hint
Use Book myBook = new Book(); to create the object.
4
Print the title and author of the Book object
In the main method, print the title and author of the myBook object using System.out.println.
Java
Hint
Use System.out.println(myBook.title); and System.out.println(myBook.author); to print the values.
Practice
(1/5)
1. What is a default constructor in Java?
easy
A. A method that returns the default value of a class.
B. A constructor with no parameters that Java provides automatically if none is written.
C. A constructor that must always be written by the programmer.
D. A special method that runs only when a program ends.
Solution
Step 1: Understand what a constructor is
A constructor is a special method used to create objects of a class.
Step 2: Identify the default constructor
If no constructor is written, Java automatically provides a constructor with no parameters called the default constructor.
Final Answer:
A constructor with no parameters that Java provides automatically if none is written. -> Option B
public class Book {
String title;
public Book() {
title = "Java Basics";
}
public Book(String title) {
title = title;
}
}
medium
A. The second constructor does not set the instance variable correctly.
B. The default constructor is missing.
C. The class has no constructors.
D. The constructors have wrong return types.
Solution
Step 1: Analyze the second constructor
It assigns parameter 'title' to itself, not to the instance variable.
Step 2: Understand correct assignment
Use 'this.title = title;' to assign parameter to instance variable.
Final Answer:
The second constructor does not set the instance variable correctly. -> Option A
Quick Check:
Use 'this' to assign constructor parameters to fields [OK]
Hint: Use 'this.' to assign parameters to instance variables [OK]
Common Mistakes:
Assigning parameter to itself instead of instance variable
Missing default constructor (actually present)
Adding return types to constructors
5. You want to create a class Person that sets the name to "Unknown" by default if no name is given. Which constructor code correctly implements this using a default constructor?
hard
A. public class Person {
String name;
public Person() {
name = name;
}
}
B. public class Person {
String name = "Unknown";
public Person(String name) {
name = name;
}
}
C. public class Person {
String name;
public Person(String name) {
this.name = name;
}
}
D. public class Person {
String name;
public Person() {
name = "Unknown";
}
public Person(String name) {
this.name = name;
}
}
Solution
Step 1: Check default constructor sets default value
public class Person {
String name;
public Person() {
name = "Unknown";
}
public Person(String name) {
this.name = name;
}
}'s default constructor sets name to "Unknown" correctly.
Step 2: Verify parameterized constructor sets name properly
public class Person {
String name;
public Person() {
name = "Unknown";
}
public Person(String name) {
this.name = name;
}
} uses 'this.name = name;' to assign parameter to instance variable.
Final Answer:
public class Person {
String name;
public Person() {
name = "Unknown";
}
public Person(String name) {
this.name = name;
}
} -> Option D
Quick Check:
Default constructor sets default value, parameterized sets given value [OK]
Hint: Default constructor sets default values; use 'this' for parameters [OK]