Programming with Java
Lesson 1 of 4 9 min +65 XP

Objects & Classes

Model things with classes in Java.

What you'll learn

  • Define a class
  • Create an object
  • Explain encapsulation
The cookie cutter

A cookie cutter is a template; each cookie stamped from it is a separate treat with its own sprinkles. In Java a class is the cutter and an object is a cookie — one blueprint, many individual things made from it.

Classes are blueprints

A class defines fields (data) and methods (behavior). An object is an instance created from that class with the new keyword.

class Dog {
  String name;
  void bark() { System.out.println("Woof"); }
}
Dog d = new Dog();  // an object

Knowledge Check

+15 XP / correct

1. In Java, you create an object with which keyword?

2. A class defines…