You’ve probably noticed this word
class in all of the programs you’ve written so far for this course. But what exactly are classes? You can think of classes as blueprints. They contain the generalized code for a non-primitive data type.
Classes have fields and behaviors. Fields are basically variables that belong to a class. They define the characteristics of that class. For example, I could have a class called
Person with fields like
address. Behaviors are what the class does—that is, the methods contained in that class. For example, the behaviors in the
Person class could include
eat(), walk(), speak(), or other things that a
Person can do.
Objects vs. Classes
Remember when we learned about Object-Oriented Programming (OOP)? So what exactly are objects? Well, objects are instances of classes. They are the concrete representation of the class.
A class is like a blueprint for a house — it defines the common features for the house, like having a door or walls or rooms. However, the house that is constructed is like the object — that specific house is like other houses (it has doors, walls, and rooms), but it might have attributes that are specific to that house. For example, it might have 5 doors and red walls, while another house might have 2 doors and white walls. In other words, a class is a plan (abstract, intangible). An object is the result of that plan (concrete, tangible).