A class is an abstract blueprint used to create more specific, concrete objects. Classes often represent broad categories, like
Dog that share attributes. These classes define what attributes an instance of this type will have, like
color, but not the value of those attributes for a specific object.
Classes can also contain functions, called methods available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific type of object.
For example, our
Carclass may have a method
repaintthat changes the
colorattribute of our car. This function is only helpful to objects of type
Car, so we declare it within the
Carclass thus making it a method.
Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like
goldenRetriever. Each object can have unique values to the properties defined in the class.
For example, say we created a class,
Car, to contain all the properties a car must have,
model. We then create an instance of a
myCarto represent my specific car.
We could then set the value of the properties defined in the class to describe my car, without affecting other objects or the class template.
We can then reuse this class to represent any number of cars.
Building blocks of OOP
Next, we’ll take a deeper look at each of the fundamental building blocks of an OOP program used above:
In a nutshell, classes are essentially user defined data types. Classes are where we create a blueprint for the structure of methods and attributes. Individual objects are instantiated, or created from this blueprint.
Classes contain fields for attributes, and methods for behaviors. In our
Dog class example, attributes include
birthday, while methods include
Here’s a code snippet demonstrating how to program a
Remember the class is a template for modelling a dog, and an object is instantiated from the class representing an individual real-world thing.
Of course, OOP includes objects! Objects are instances of classes created with specific data, for example in the code snippet below
Rufus is an instance of the
When the new class
Dog is called:
- A new object is created named
- The constructor runs
birthdayarguments, and assigns values
Objects have states and behaviors. State is defined by data: things like names, birthday, and other information you’d want to store about a dog. Behaviors are methods, the object can undertake.
|N/A||What is it?||Information Contained||Actions||Example|
|Classes||Blueprint||Attributes||Behaviors defined through methods||Dog Template|
|Objects||Instance||State, Data||Methods||Rufus, Fluffy|
Attributes are the information that is stored. Attributes are defined in the
Class template. When objects are instantiated individual objects contain data stored in the Attributes field.
The state of an object is defined by the data in the object’s attributes fields. For example, a puppy and a dog might be treated differently at pet camp. The birthday could define the state of an object, and allow the software to handle dogs of different ages differently.
Methods represent behaviors. Methods perform actions; methods might return information about an object, or update an object’s data. The method’s code is defined in the class definition.
When individual objects are instantiated, these objects can call the methods defined in the class. In the code snippet below, the
bark method is defined in
Dog class, and the
bark() method is called on the
Methods often modify, update or delete data. Methods don’t have to update data though. For example the
bark() method doesn’t update any data because barking doesn’t modify any of the attributes of the
updateAttendance() method adds a day the
Dog attended the pet sitting camp. The attendance attribute is important to keep track of for billing Owners at the end of the month.
Methods are how programmers promote reusability, and keep functionality encapsulated inside an object. More on encapsulation and data abstraction in the Principles of OOP section. This reusability is a great benefit when debugging. If there’s an error, there’s only one place to find it and fix it instead of many.
The underscore in
_attendance denotes that the variable is protected, and shouldn’t be modified directly. The
updateAttendance() method is used to change