Anatomy of a Class

class anatomy

Part of: Writing Classes

What a Class Is A class is a blueprint that describes a kind of object. It bundles together two things: state (the data an object remembers) and behavior (the actions an object can perform). Every object you create from the class is an instance of it, and each instance carries its own copy of the state. Think of a class like an architectural plan for a house. The plan is not a house you can live in, but from one plan you can build many houses. Each house (object) has its own paint color and furniture (state) even though they all share the same floor plan (class). The Parts of a Class A typical AP CSA class has four sections, written in this order by convention: - Instance variables : the fields that hold each object's state, usually private. - Constructors : special methods that initialize a new object. - Accessor and mutator methods : let outside code safely read or change the state. - Other methods : the behaviors that operate on the state. Objects vs. Classes You never run a class directly. Instead you build objects from it using the new keyword: Here Dog is the class (the blueprint) and d refers to one object (a built instance). The variable d stores a reference to the object,

Challenge: Greet a Person