These notes recap Topic 3.3. For worked examples, read the study guide.
Recap
- A class has a header, instance variables, constructors and methods.
- In this course classes are always
public class Nameand constructors are alwayspublic. - Instance variables are
privateunless the specification says otherwise. Each object has its own copy of every instance variable. privaterestricts access to the declaring class.publicallows access from classes outside it.- Encapsulation hides implementation details: outside code uses public methods and never touches private data directly.
- Public methods can be called inside or outside the class. Private methods can only be called from inside it, so they are used as helpers.
Key relationships
| Member | Typical form | Who can use it |
|---|---|---|
| Class | public class Parcel |
Everyone |
| Instance variable | private double weightKg; |
Code inside Parcel |
| Constructor | public Parcel(double w, String p) |
Everyone (to create objects) |
| Behaviour other classes need | public boolean isHeavy() |
Everyone |
| Helper | private boolean isValid(int points) |
Code inside the class |
Assumptions
- A constructor has the class’s name and no return type, not even
void. - If no constructor is written, Java supplies a default no-parameter constructor (Topic 3.4).
- Access errors are compile-time errors, for example
weightKg has private access in Parcel.
Worked reminder
For Parcel a = new Parcel(25.5, "Leeds"); Parcel b = new Parcel(3.0, "Bath"); a.markDelivered();, a.describe() is "Leeds (delivered)" and b.describe() is "Bath (on the way)": marking a does not change b. For a Scoreboard, after addHome(3), addAway(5), addAway(-2) and addHome(2) both scores are 5, so leader() returns "tied".
Reading a class quickly in the exam
- Find the header, then list the instance variables and their types: this is the object’s state.
- Find the constructor: what initial values does it give each variable?
- For each method, note whether it only reads the state (an accessor) or changes it.
- Check every line that uses a member against its
publicorprivateaccess and the class it is written in. - When code creates two objects, keep a small state table for each, as in the study guide, and update it line by line.
Mistakes to avoid
- Writing
voidbefore a constructor name. - Declaring instance variables
publicand then accessing them from another class. - Calling a private method from another class.
- Forgetting that a class with no instance-variable assignment in the constructor leaves defaults (0, 0.0,
false,null). - Believing that changing one object changes all objects of the class.
Quick self-check
- Which keyword makes a method callable only from inside its class? Answer:
private. - Why make instance variables private? Answer: so other classes cannot change an object’s state except through public methods, which keeps its data safe (encapsulation).
- What is wrong with
public void Cup(int ml)in classCupif it is meant to be a constructor? Answer:voidmakes it an ordinary method; a constructor has no return type. - Two
Parcelobjects exist. How many copies ofweightKgare there? Answer: two, one per object.