Beginner level · Module 4 of 5 · 11 guided hours
Classes and Objects in Java
Model real things as classes with fields, constructors and methods, and protect data with encapsulation.
Learning objectives
- Write classes with fields, constructors and instance methods
- Create and use objects, including lists of objects
- Apply encapsulation with private fields and validated setters
Topics
- class vs object
- fields and constructors
- this keyword
- instance vs static
- private fields, getters and setters
- toString
Lessons
-
Lesson 1 Your First Class
Define a class with fields and methods, create objects with new, and call methods on them.
classobjectfieldinstance method
-
Lesson 2 Constructors and the this Keyword
Give objects valid starting values with constructors, use this to refer to the current object, and know when to use static.
constructorthisconstructor overloadingstatic members
-
Lesson 3 Encapsulation: Protecting an Object's Data
Make fields private and control changes through methods that check the rules, so objects can never hold invalid data.
privategetter and settervalidationinvariant
Practical activity
Model a mobile-money wallet as a class with deposit, withdraw and balance methods that never allow a negative balance, and test it from main.
Expected outcomes
- Working classes that keep their own data valid
- Programs that create and use several objects
Assessment
Code review of the wallet class against a checklist (private fields, validation, clear method names) plus a short oral explanation of how an object differs from a class.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.