Object-Oriented Programming through Java · Classes and Objects
Classes and Objects in Code
In Object-Oriented Programming through Java because Unit I named the ticket; this lesson writes the Ticket class, makes objects, and decides who may touch the fields.
Meera's desk now writes Ticket in a .java file. A class names the shared idea; new makes one ticket; a second variable can point at the same ticket; access words decide who may read the seat. The next lesson builds that object and writes its methods.
- Object-Oriented Programming through Java
- Easy level
- 6 concepts
1A class declaration writes the mould
A class declaration is the Java sentence that names the mould. class Ticket { } says: there is a kind of thing called Ticket, and its members will live between the braces.
The file Ticket.java holds that class. You do not yet have a ticket in the hall — you have the idea written down so the machine can print many.
Figure. The file on the left is the place. The class on the right is the mould. No object exists yet.
How it works
- A class declaration writes the mouldclass Name { } writes the mould. It is not yet a ticket in anyone's hand.
The smallest Ticket class
public class Ticket {
// members go here
}2Members are the facts and the work
Members live inside the class. A field is a fact the object will hold — show, seat, price. A method is work the object can do — print the stub, apply a student cut.
Fields and methods are both members. The difference is what they are for: stored facts versus behaviour.
Figure. Ticket owns two kinds of member. Fields store. Methods do work. Both sit inside the same braces.
How it works
- Members are the facts and the workA field is a fact. A method is work. Both are members of the class.
Three fields and one method
public class Ticket {
String show;
String seat;
int price;
void printStub() {
System.out.println(seat + " " + price);
}
}3new makes one object
new Ticket() asks the machine for one object of that class. Meera's variable t holds a reference to that object — a way to find it — not the paper itself.
A second new Ticket() is a second object. Two news, two tickets, even if both start with the same show.
Figure. The class is one. Each new arrow is a separate object. t and u do not share paper.
How it works
- new makes one objectnew makes one object. Another new is another object.
Two objects from one class
Ticket t = new Ticket();
t.seat = "A12";
Ticket u = new Ticket();
u.seat = "B7";Coding lab. Make one ticket and print its seat runs in the app, with checks on your output.
4Assignment copies the reference
Ticket u = t; does not print a second stub. It copies the reference: u now finds the same object t finds.
After that, u.seat = "C4" changes Meera's ticket. There is still one object. Two names, one seat.
Figure. Both variables point at the same object. Changing the seat through u is visible through t.
u = t shares the object
Ticket t = new Ticket();
t.seat = "A12";
Ticket u = t;
u.seat = "C4";
// t.seat is now C4 as wellOne seat after assignment
t is a new Ticket with seat A12. Then u = t, then u.seat = C4. What does t.seat hold?
- new Ticket(); t.seat = A12one object, seat A12
- u = tu and t find that same object
- u.seat = C4t.seat is C4
Pro tip. If you needed a second stub, you needed a second new, not an assignment.
5Access words decide who may touch a member
Access control is a word on a member: public, private, protected, or none. It answers who may read or write that member — not who may hold the object.
public is the desk window. private is the drawer behind the desk. No word means the rest of the same package may see it. protected also lets a subclass see it.
Figure. The window is what other classes may call. The drawer is only for Ticket's own methods. The two panels do not overlap: a caller is on one side or the other.
| Word | Who may use the member |
|---|---|
| public | any class |
| protected | same package, or a subclass |
| (none) | same package only |
| private | this class only |
6Private fields, public methods
A private field cannot be read as t.price from the desk class. Ticket's own methods still can. That is encapsulation written in Java: the facts stay inside; the public methods are the only door.
Meera's desk asks t.priceAfterTax() instead of writing t.price from outside. The formula can change; the door stays.
Figure. price is private: the desk cannot read t.price. Ticket's own methods still can. The public door is priceAfterTax(). The facts stay inside.
How it works
- Private fields, public methodsMake fields private. Offer a small public method. That is access control doing encapsulation.
price is private; the door is a method
public class Ticket {
private int price;
public int priceAfterTax() {
return price + price / 10;
}
}Notes
- In Object-Oriented Programming through Java because Unit I named the ticket; this lesson writes the Ticket class, makes objects, and decides who may touch the fields.
- new Ticket() makes one object. Assigning one Ticket variable to another copies the reference, not a second paper ticket.
- private fields stay inside the class. Public methods are the small surface the desk offers.
Exam traps & shortcuts
- If two variables print the same seat after you change one, they share one object. That is assignment of a reference.
- A field with no access word is package-private, not private.
Reference tables
Access is decided on the member, not on the object. The same Ticket object looks different from inside Ticket, from StudentTicket, and from the desk in another package.
| Word | Same class | Same package | Subclass | Elsewhere |
|---|---|---|---|---|
| private | yes | no | no | no |
| (none) | yes | yes | no | no |
| protected | yes | yes | yes | no |
| public | yes | yes | yes | yes |
Recap
A class is the mould in a file. new makes one object. Assignment shares it. Access words decide the door.
- class writes the mould
- class Ticket { } is the idea. It is not yet a ticket.
- new makes one object
- A second new is a second object.
- u = t shares
- Assignment copies the reference. Two names, one seat.
- private is the drawer
- Other classes use public methods, not t.price.
Practise Classes and Objects in Code
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