B3.1.4 · OOP for a single class
Instantiation & constructors
By the end you can
Construct and trace programs using new, parameterized constructors, constructor overloading, and the this keyword.
Before an object can be used, its memory must be allocated and its internal attributes set to valid initial values. This initialization process is called instantiation, and it is handled by a special method known as a.
Constructor characteristics#
In languages like Java and C++, constructors adhere to three strict rules:
- Identical Name: A constructor must have the exact same name as the class (including uppercase letters).
- No Return Type: Constructors never specify a return type, not even
void. - Automatic Invocation: You cannot call a constructor like a normal method; it is executed automatically when you use the
newkeyword.
// Java constructor declaration and invocation
public class Book {
private String title;
private double price;
// Constructor
public Book(String title, double price) {
this.title = title;
this.price = price;
}
}
// Instantiation:
Book myBook = new Book("Computer Science", 45.00);The role of this#
In the example above, the constructor parameter is named title and the instance field is also named title. The parameter shadows the field! Using this.title explicitly tells the compiler: “Assign to the title attribute of this specific object instance.” In Python, the equivalent is explicitly passing self as the first parameter (def __init__(self, title): self.title = title).
Constructor overloading#
A class can provide multiple constructors as long as their parameter lists differ in number or types (overloading). For instance, a class can have a default constructor with no parameters setting sensible defaults, and a parameterized constructor accepting custom values.
Check yourself
Constructors
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 8 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Describe the purpose, naming rules, execution timing, and return type characteristics of a constructor in object-oriented programming.
[4]Describe: A detailed account: say what happens, step by step if it is a process.
2.Explain the purpose of the 'this' keyword in a Java constructor (e.g. this.name = name).
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.State what a default constructor is and explain when a compiler provides one automatically.
[2]State: Give a short answer, no explanation needed.
In a nutshell
- A constructor is a special method called automatically when an object is instantiated using the new operator.
- Constructors share the exact name of their class and have no return type.
- Overloaded constructors allow objects to be initialized with different parameter combinations.
- The this keyword refers to the current executing object instance, resolving shadowing between parameters and fields.
