A class is best described as:
Classes and Objects
Classes as blueprints, instance variables and methods, constructors and overloading, information hiding and encapsulation (private + getters/setters), this, static variables and methods, toString, equals, copy constructors and composition (has-a), built up through three versions of a Student class.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 40 slides
- 1 Classes and Objects
- 2 Learning Outcomes
- 3 Class and Method Definitions
- 4 Class
- 5 Class and Method Definitions
- 6 Class and Method Definitions
- 7 Class and Method Definitions
- 8 Book Reading - Walter Savitch book ch 5 (5.1)
- 9 Class
- 10 Class Student
- 11 Class and Instance Variables
- 12 Constructor
- 13 Defining Constructors
- 14 Constructors
- 15 Information Hiding
- 16 Information Hiding
- 17 The and Modifiers
- 18 Data hiding and data Validation
- 19 Encapsulation
- 20 Encapsulation
- 21 Using object-oriented design (Student class)
- 22 this The Keyword
- 23 this The Keyword
- 24 Static keyword
- 25 Static Variables
- 26 Static Variables
- 27 Static Methods
- 28 Programming Example- version 2
- 29 Student Class Code
- 30 toString Method
- 31 equals method
- 32 equals method
- 33 Copy Constructors
- 34 Copy Constructors
- 35 Static Methods
- 36 Slide 36
- 37 Slide 37
- 38 Composition
- 39 Student Version 3
- 40 Slide 40
Class
Notes
A class is the blueprint; an object is one instance built from it.
Exam angle
Class
Notes
Attributes (instance variables) hold an object’s state; methods are its behaviour.
Constructor
Notes
Constructor: same name as the class, NO return type (not even void), runs when you call new.
Trap Writing a return type (even void) makes it an ordinary method, not a constructor.
Exam angle
Which is true of a constructor?
Defining Constructors
Notes
If you write no constructor, Java supplies a no-argument default constructor.
Constructors
Notes
Constructor overloading: several constructors with different parameter lists.
Data hiding and data Validation
Notes
Data hiding lets setters VALIDATE values (e.g., reject a negative age).
Exam angle
Why should instance variables be private?
Compare with the model answer
So other classes can only change them through methods, which can validate the values (data validation) and hide the implementation; the class can then change internally without breaking its users.
Encapsulation
Notes
Encapsulation: hide the details, show only the interface (like a car’s pedals).
Encapsulation
Notes
Recipe: private instance variables + public accessors (getters) and mutators (setters).
this The Keyword
Notes
this = the current object. Use it when a parameter has the same name as a field: this.name = name; and to call another constructor: this(...).
Exam angle
In a constructor with parameter name, which assigns the field?
Static keyword
Notes
static = one shared copy for the whole class; final = constant.
Static Variables
Exam angle
A counter of how many Student objects have been created should be:
Static Methods
Notes
Static methods belong to the class, not an object (Math.max). They can’t use this or instance variables directly.
Exam angle
A static method can directly access instance variables.
- It has no object (no this).
toString Method
Notes
toString() returns a String describing the object; println(obj) calls it automatically.
equals method
Notes
Trap == on objects compares addresses; equals (if overridden) compares contents.
Copy Constructors
Notes
Copy constructor: Student(Student s) builds a separate object with the same values.
Composition
Notes
Memory trick is-a → inheritance; has-a → composition.
Exam angle
A Student class with a Date birthDate field is an example of: