CS102 · Classes · Annotated SlidesClasses and Objects
Original
Overview

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. 1 Classes and Objects
  2. 2 Learning Outcomes
  3. 3 Class and Method Definitions
  4. 4 Class
  5. 5 Class and Method Definitions
  6. 6 Class and Method Definitions
  7. 7 Class and Method Definitions
  8. 8 Book Reading - Walter Savitch book ch 5 (5.1)
  9. 9 Class
  10. 10 Class Student
  11. 11 Class and Instance Variables
  12. 12 Constructor
  13. 13 Defining Constructors
  14. 14 Constructors
  15. 15 Information Hiding
  16. 16 Information Hiding
  17. 17 The and Modifiers
  18. 18 Data hiding and data Validation
  19. 19 Encapsulation
  20. 20 Encapsulation
  21. 21 Using object-oriented design (Student class)
  22. 22 this The Keyword
  23. 23 this The Keyword
  24. 24 Static keyword
  25. 25 Static Variables
  26. 26 Static Variables
  27. 27 Static Methods
  28. 28 Programming Example- version 2
  29. 29 Student Class Code
  30. 30 toString Method
  31. 31 equals method
  32. 32 equals method
  33. 33 Copy Constructors
  34. 34 Copy Constructors
  35. 35 Static Methods
  36. 36 Slide 36
  37. 37 Slide 37
  38. 38 Composition
  39. 39 Student Version 3
  40. 40 Slide 40
Slide 1 / 40

Classes and Objects

Slide 1: Classes and Objects
Slide 2 / 40

Learning Outcomes

Slide 2: Learning Outcomes
Slide 3 / 40

Class and Method Definitions

Slide 3: Class and Method Definitions
Slide 4 / 40

Class

Slide 4: Class

Notes

  1. A class is the blueprint; an object is one instance built from it.

Exam angle

PredictionQ1

A class is best described as:

Slide 5 / 40

Class and Method Definitions

Slide 5: Class and Method Definitions
Slide 6 / 40

Class and Method Definitions

Slide 6: Class and Method Definitions
Slide 7 / 40

Class and Method Definitions

Slide 7: Class and Method Definitions
Slide 8 / 40

Book Reading - Walter Savitch book ch 5 (5.1)

Slide 8: Book Reading - Walter Savitch book ch 5 (5.1)
Slide 9 / 40

Class

Slide 9: Class

Notes

  1. Attributes (instance variables) hold an object’s state; methods are its behaviour.

Slide 10 / 40

Class Student

Slide 10: Class Student
Slide 11 / 40

Class and Instance Variables

Slide 11: Class and Instance Variables
Slide 12 / 40

Constructor

Slide 12: Constructor

Notes

  1. Constructor: same name as the class, NO return type (not even void), runs when you call new.

  2. Trap Writing a return type (even void) makes it an ordinary method, not a constructor.

Exam angle

PredictionQ1

Which is true of a constructor?

Slide 13 / 40

Defining Constructors

Slide 13: Defining Constructors

Notes

  1. If you write no constructor, Java supplies a no-argument default constructor.

Slide 14 / 40

Constructors

Slide 14: Constructors

Notes

  1. Constructor overloading: several constructors with different parameter lists.

Slide 15 / 40

Information Hiding

Slide 15: Information Hiding
Slide 16 / 40

Information Hiding

Slide 16: Information Hiding
Slide 17 / 40

The and Modifiers

Slide 17: The and Modifiers
Slide 18 / 40

Data hiding and data Validation

Slide 18: Data hiding and data Validation

Notes

  1. Data hiding lets setters VALIDATE values (e.g., reject a negative age).

Exam angle

PredictionQ1

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.

Slide 19 / 40

Encapsulation

Slide 19: Encapsulation

Notes

  1. Encapsulation: hide the details, show only the interface (like a car’s pedals).

Slide 20 / 40

Encapsulation

Slide 20: Encapsulation

Notes

  1. Recipe: private instance variables + public accessors (getters) and mutators (setters).

Slide 21 / 40

Using object-oriented design (Student class)

Slide 21: Using object-oriented design (Student class)
Slide 22 / 40

this The Keyword

Slide 22: this The Keyword

Notes

  1. 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

PredictionQ1

In a constructor with parameter name, which assigns the field?

Slide 23 / 40

this The Keyword

Slide 23: this The Keyword
Slide 24 / 40

Static keyword

Slide 24: Static keyword

Notes

  1. static = one shared copy for the whole class; final = constant.

Slide 25 / 40

Static Variables

Slide 25: Static Variables

Exam angle

PredictionQ1

A counter of how many Student objects have been created should be:

Slide 26 / 40

Static Variables

Slide 26: Static Variables
Slide 27 / 40

Static Methods

Slide 27: Static Methods

Notes

  1. Static methods belong to the class, not an object (Math.max). They can’t use this or instance variables directly.

Exam angle

PredictionQ1

A static method can directly access instance variables.

Slide 28 / 40

Programming Example- version 2

Slide 28: Programming Example- version 2
Slide 29 / 40

Student Class Code

Slide 29: Student Class Code
Slide 30 / 40

toString Method

Slide 30: toString Method

Notes

  1. toString() returns a String describing the object; println(obj) calls it automatically.

Slide 31 / 40

equals method

Slide 31: equals method

Notes

  1. Trap == on objects compares addresses; equals (if overridden) compares contents.

Slide 32 / 40

equals method

Slide 32: equals method
Slide 33 / 40

Copy Constructors

Slide 33: Copy Constructors

Notes

  1. Copy constructor: Student(Student s) builds a separate object with the same values.

Slide 34 / 40

Copy Constructors

Slide 34: Copy Constructors
Slide 35 / 40

Static Methods

Slide 35: Static Methods
Slide 36 / 40

Slide 36

Slide 36: Slide 36
Slide 37 / 40

Slide 37

Slide 37: Slide 37
Slide 38 / 40

Composition

Slide 38: Composition

Notes

  1. Memory trick is-a → inheritance; has-a → composition.

Exam angle

PredictionQ1

A Student class with a Date birthDate field is an example of:

Slide 39 / 40

Student Version 3

Slide 39: Student Version 3
Slide 40 / 40

Slide 40

Slide 40: Slide 40