Object-Oriented Programming through Java · Classes and Objects
Constructors and Methods
In Object-Oriented Programming through Java because a Ticket object must be built, and the desk must call work on it — constructors, overloading, this, nested types, final and…
new Ticket() now has a job: a constructor fills show, seat and price so Meera never holds a blank stub. Methods do the work. The same name can take different lists. this means this object. A class can sit inside another. final and static change who may change a member or override a method.
- Object-Oriented Programming through Java
- Easy level
- 7 concepts
1A constructor runs when new runs
A constructor is a special method whose name is the class name and that names no return type. It runs as part of new, before anyone uses the object.
Ticket(String seat, int price) lets Meera write new Ticket("A12", 200) and know the stub is filled. Without a constructor you wrote, Java offers an empty one that sets nothing useful.
Figure. A constructor has the class name and no return type. It runs as part of new Ticket("A12", 200), before anyone uses the object, and fills the stub.
How it works
- A constructor runs when new runsA constructor has the class name and no return type. It runs because of new.
Constructor fills the stub at new
public class Ticket {
private String seat;
private int price;
public Ticket(String seat, int price) {
this.seat = seat;
this.price = price;
}
}2The same name, a different list
Overloading is two methods or constructors that share a name and differ in the parameter list — count or types. The compiler picks the match from the call you wrote.
Ticket(String seat) can default the price. printStub() and printStub(boolean withGst) are two methods, not one method that guesses.
No diagram — the idea is carried by the prose or the Java listing.
| Same | Must differ |
|---|---|
| Name | Parameter list |
| Ticket / printStub | Count or types of arguments |
Two constructors, one name
public Ticket(String seat) {
this(seat, 200);
}
public Ticket(String seat, int price) {
this.seat = seat;
this.price = price;
}3this means this object
this is a reference to the object whose method or constructor is running. this.seat is that object's seat, even when a parameter is also named seat.
this(seat, 200) is a call to another constructor of the same class. It must be the first statement in that constructor.
Figure. this is a reference to the object whose constructor is running. this.seat is that object's seat, even when a parameter is also named seat.
How it works
- this means this objectthis is this object. this.field wins when a parameter reuses the field name.
this.seat is the field; seat is the argument
public Ticket(String seat, int price) {
this.seat = seat;
this.price = price;
}4A method can call itself
A method is work you name and call. Meera's printStub() uses this object's fields and returns.
Recursion is a method calling itself on a smaller piece of the same job. countdown(3) prints 3, then calls countdown(2), and must stop at a base case or it never returns.
Figure. countdown(3) prints 3, then calls countdown(2). Recursion is a method calling itself on a smaller piece. It must stop at a base case or it never returns.
Base case first, then the smaller call
void countdown(int n) {
if (n <= 0) {
return;
}
System.out.println(n);
countdown(n - 1);
}countdown(3)
What does countdown(3) print, in order?
- countdown(3)prints 3, calls 2
- countdown(2)prints 2, calls 1
- countdown(1)prints 1, calls 0
- countdown(0)returns, prints nothing
Pro tip. The base case is not optional. Without it the calls never stop.
5A class can live inside a class
A nested class is a class declared inside another. Ticket.Stub can hold the printed line without becoming a top-level type of its own.
A static nested class does not need a Ticket instance. An inner (non-static) class holds a hidden reference to the outer object and may use its fields.
Figure. Stub is nested inside Ticket. A static nested class does not need a Ticket instance. Nest a class when it only makes sense beside the outer type.
How it works
- A class can live inside a classNest a class when it only makes sense beside the outer type. static nested needs no outer instance.
Stub is nested inside Ticket
public class Ticket {
private String seat;
static class Stub {
String line;
Stub(String line) { this.line = line; }
}
}6final means this binding will not change
final on a variable or field means the reference or value is assigned once. final int GST = 10; cannot be moved to 18 later.
final on a class means no subclass. That is a later lesson's inheritance rule; here it is the same word: this declaration is closed.
Figure. final int GST = 10 cannot be moved to 18 later. final on a field is write-once. The seat field is assigned once in the constructor.
How it works
- final means this binding will not changefinal on a field is write-once. final on a class forbids a subclass.
GST_PERCENT and seat are assigned once
public class Ticket {
public static final int GST_PERCENT = 10;
private final String seat;
public Ticket(String seat) {
this.seat = seat;
}
}7static belongs to the class; overriding rewrites in a subclass
A static method is called on the class — Ticket.gstOn(200) — and has no this. A final method may not be rewritten in a subclass.
Overriding is a subclass method with the same signature as one in the parent. StudentTicket can override priceAfterTax() so a student stub uses a different rule. The next unit makes inheritance the whole lesson; here the contrast is: overload is a different list, override is the same list in a child class.
No diagram — the idea is carried by the prose or the Java listing.
| Word | What it changes |
|---|---|
| overload | same name, different parameter list |
| override | same signature in a subclass |
| static | belongs to the class; no this |
| final method | a subclass may not override it |
static has no this; an instance method may later override
public static int gstOn(int price) {
return price / 10;
}
public int priceAfterTax() {
return price + gstOn(price);
}Notes
- In Object-Oriented Programming through Java because a Ticket object must be built, and the desk must call work on it — constructors, overloading, this, nested types, final and static.
- A constructor has the class name and no return type. Overloading is the same name with a different parameter list.
- this.seat is this object's seat. A static method belongs to the class. A final method cannot be overridden in a subclass.
Exam traps & shortcuts
- If you write any constructor, Java stops writing the empty one. new Ticket() then fails unless you still offer a no-arg form.
- Overloading is decided at compile time by the argument list. Overriding is decided at run time by the object's class.
Recap
new runs a constructor. Names can overload. this is this object. Nested, final and static change the shape of members.
- constructor at new
- Class name, no return type. If you write one, the empty one goes away.
- overload vs override
- Different list: overload. Same list in a child: override.
- this
- this.seat is the field. this(...) must be first.
- static has no this
- Call Ticket.gstOn(200). A final method cannot be overridden.
Practise Constructors and Methods
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device