Intro | Anatomy of a Class | Constructors | Accessor Methods | Mutator Methods | Static Variables |
Unit 5 Classes - Constructors
Introduction to Constructors in Java
What are Constructors?
In Java, a constructor is a special method used to initialize objects. The constructor is called when an object of a class is created. It has the same name as the class and does not return any value, not even void
.
Key Points:
- Same name as class: The constructor must have the same name as the class.
- No return type: Constructors do not return any value.
- Used to initialize objects: They set initial values for object attributes.
Types of Constructors
1. Default Constructor:
A constructor that does not accept any parameters. Java provides a default constructor if you don’t define any.
class Car {
String brand;
// Default constructor
Car() {
brand = "Unknown";
}
}
Car myCar = new Car();
System.out.println(myCar.brand); // Outputs: Unknown
Unknown
2. Parameterized Constructor:
A constructor that takes parameters to initialize object attributes with custom values.
class Car {
String brand;
// Parameterized constructor
Car(String b) {
brand = b;
}
}
Car myCar = new Car("Toyota");
System.out.println(myCar.brand); // Outputs: Toyota
Toyota
3. Constructor Overloading
Constructor Overloading
In Java, you can have more than one constructor in a class. This is known as constructor overloading. Each constructor must have a different parameter list.
class Car {
String brand;
int year;
// Default constructor
Car() {
brand = "Unknown";
year = 0;
}
// Parameterized constructor
Car(String b, int y) {
brand = b;
year = y;
}
}
Car car1 = new Car();
Car car2 = new Car("Ford", 2020);
System.out.println(car1.brand + " " + car1.year); // Outputs: Unknown 0
System.out.println(car2.brand + " " + car2.year); // Outputs: Ford 2020
Unknown 0
Ford 2020
Escape Room: Constructor Puzzle
You’re trapped in a room and the only way out is to unlock a door using your Java constructor knowledge!
Rules:
- Each clue is a code snippet related to constructors. Solve the problems to reveal the next clue.
- Once you complete all tasks, you will receive the “escape” message.
Clue 1:
The constructor below is missing a key part. Complete the code so that it runs correctly.
// CLUE 1
class Door {
String lockCode;
// Add the correct constructor here
Door(_____ lock) {
// Constructor body
lockCode = _____;
}
}
Door escapeDoor = new Door("1234");
System.out.println(escapeDoor.lockCode); // Should print: 1234
//CLUE 2
class Room {
String name;
int area;
// Constructor 1
Room(String name) {
this.name = name;
}
// Constructor 2 (missing)
Room(String name, int area) {
// Complete this constructor
// Hint: Use the 'this' keyword
}
}
Room escapeRoom = new Room("Puzzle Room", 500);
System.out.println("Room: " + escapeRoom.name + ", Area: " + escapeRoom.area);
// Should print: Room: Puzzle Room, Area: 500
//CLUE 3
class Key {
String keyType;
// Default constructor (to be completed)
Key() {
// Hint: Initialize keyType to a default value, e.g., "Unknown"
}
// Parameterized constructor
Key(String keyType) {
this.keyType = keyType;
}
}
Key defaultKey = new Key();
Key customKey = new Key("Golden Key");
System.out.println("Default Key: " + defaultKey.keyType); // Should print: Unknown
System.out.println("Custom Key: " + customKey.keyType); // Should print: Golden Key