AS/A Computer Science: Fundamentals of data structures

Abstract data types

Abstract data types are created with elementary and composite data types alongside rules that specify how the user can interact with them. They include things like queues, stacks, graphs, trees and hash tables.

Abstract data types and OOP

An effective way to create abstract data structures is using object oriented programming (OOP). OOP is a way of designing programs based around objects which each have attributes and methods.

Attributes are like variables that belong to the object e.g. an array of X length. Methods are like functions that can have an effect on the attributes of the object



An example using OOP to create a record class



Let's look at defining the record above as a class using OOP.

Defining the class and the constructor method

A class is defined using the class statement and by supplying a name followed by brackets.

The first method defined in a class is the known as the constructor method. It is called each type a new instance of the class is created. This defines the attributes of this instance of a student as those passed as parameters when it is created e.g. studentA = Student("Bob", "Dobalina", 19/11/1991, 1234). Each new student that is defined will take its own parameters.



The constructor is what is known as a magic method and always has a double underscore before and after init. One of the more confusing aspects of OOP to begin with is this notion of self. Self refers to this particular instance of an object. The other parameters are the values the user needs to provide when creating a new student record object i.e. their name, date of birth and student number. The line self.forename = forename means for this instance of a student record make the forename equal to the one that was passed in. The same is true of the next three lines. They each set the value for this instance of a student record equal to the values passed for each parameter.


Understanding objects

The code below creates a student object with the passed values and stores it in the variable studentA. Note that if we try to print the variable we are unable to see the details of the object - this is known as information hiding. While we are able to access data from the objects using dot notation this is considered bad practise in OOP where an objects attributes should only be accessed or changed using its methods.



Getters and setters

Some of the most common methods to see in an object are the getters and setters. Getters, sometimes called accessor methods, return the current value of an attribute. Setters, sometimes called mutator methods, allow you to change the value of an attribute. In this example both have been supplied for the name attributes as a student's preferred name may change but date of birth and student number will remain the same so no method has been made to update them.



With the appropriate getters and setters in place we can access and ammend our object values via their methods using dot notation as shown below.



Putting it into practice

Ive made a repl with this student class creaed and a series of chalenges for working with the class. Download the start file here.

The video below goes through the solution to the challenges.


© All materials created by and copyright S.Goff