Classes and Objects

A class is a template used to create objects within code. Classes are the
foundational elements of Object Orientated Programming (OOP). Imagine like a blueprint for a house, the blueprint isn’t a house but is used to create a house object. When you create an object from a class it is referred to as an instance of that class, such as an instance of a house from a blueprint. Houses can include variables and functions. Functions within a class are called methods, but they are the same as functions/subroutines.

Classes and objects can be used to create code that acts similar, or has similar features, but with variations. You could have a class called ‘Dog’ which stores the names of the dog, the number of legs, and the species. Every dog has a different name, but it is still a dog. A dog may have 4 legs, or it may have 3, but it is still and instance of a Dog.

The below code shows the definition of a Dog class with three fields, or variables, one method and a constructor. The code snippets are written in the programming language Java, which means the syntax may differ across languages but the concepts will be the same. We will looks through each line of this code and explain exactly what is happening here.

The first line in the code snippet defines the class with the name ‘Dog’, everything within the outer curly brackets is the body of the class, and is what makes up our template of a Dog.

Lines 3 to 5 define the fields, or variables within our Dog class. The public modifier means that we can access this fields outside of the class, when we create instances of the Dog class, and the private modifier means we cannot access the field outside of the class. The static modifier makes a variable shared across all instances. For example, all dogs will have the same species and if one dog object changes that static field it will change for all other instances, whereas the number and legs and name of each instance will be different per instance.

The ‘species’ is a string variable and is static, and shared across all instance of the class, and is also public which means we can access it outside the class. The ‘numberLegs’ field is an integer variable type, and is public so we can access it from outside the class, but it is not static which means all instances can have different values inside their own version of this variable. The last variable is called ‘name’, it is a String, it is private which means we cannot directly access is outside the class and it is not static.

The next section of our code is the constructor function. This function, or method, is call whenever we create a new instance of the Dog class. Think of it like the mandatory setup of each Dog, what is required and must be initilised when a Dog is created. Within our constructor function we do not declare a return value, as it does not and cannot return any variable. It does take one parameter, a String variable must be passed into the constructor.

The constructor takes the parameter variable ‘name’ and assigns its own internal ‘name’ variable to it’s value. This means the variable we give to the constructor, the name of the newly created Dog, will be assigned to an internal variable within the class (the variable declared on line 5). We also initilise the internal field ‘numberLegs’ with the value of 4.

The last part of our class defines a public method, a method that we can access from outside the class, called ‘getName’ that return a String variable type. This method returns the classes internal variable called ‘name’, this is an example of a getter method. Calling this function will return the name of the dog that we set within the constructor.

This method is public, and therefore can be called outside the class. This method allows us to access the private field ‘name’ that is defined within the class on line 5.

The final bit of our code snippets is written outside of our class and is used to interact with the class. The first line defines a variable called ‘fido’ that is a Dog variable type, and initilises the variable to a new Dog and passes the string “Fido” to the classes constructor. The new keyword is used to create a new instance of the Dog class, and effectively calls the constructor, which is why we must pass a String variable into the creation of the variable.

The next three lines interact with that newly created instance object. We call the ‘getName’ method of the instance, we can tell it is a call to a method due to the brackets ‘()’ used. This method doesn’t take any parameters, so the brackets are empty. This line will print the word “Fido” as that is the return value of that method.

The next two lines directly access fields from our instances. The first is the ‘numberLegs’ field, which is a non-static variable. the second is the ‘species’ field that is static. That means the numberLegs field could be different across instance of the Dog class, but the ‘species’ field is the same through all instances.

The below code snippet shows how static and non-static fields work. First we create a new instance of the Dog class called ‘buddy’ and pass a different name to the constructor. We then change the ‘numberLegs’ and ‘species’ fields of our new buddy variable. The print statements then print out those two fields, ‘numberLegs’ and ‘species’ from both our ‘fido’ instance and our ‘buddy’ instance. The number of Legs will be the number 4 for ‘fido’ but the number 3 for ‘buddy’, because it is non-static. The field ‘species’ will print “Mammal” for both instances, because it is a static variable and therefore shared across all instances.

Leave a Reply