Classes and Objects

Course Home

Some people talk about classes and objects as being the same, but they are not. However, they are very intertwined. A class is a template for an object. An object is an instance of a class. So as we write code we are creating classes, and when the code is running instances of those classes are objects.

Example: we have a class called Car. Objects of that class could be Seat, Ferrari, Ford. Example: we have a class called FootballTeam. Objects of that class could be Chelsea, Arsenal and the best team Manchester United.

You can see classes as a blueprint. Within the blueprint, we define properties. Those properties are the values that can be set on each object. So for our example of a car, we could have properties like manufacturer, engine Size, colour, number of doors and sunroof. Then when we create an instance of the Car class, we can set those properties. So for my personal car we’d have Seat, 1299cc, Silver, 5, no. We wouldn’t say ‘no’ in code though, we’d say false. We’ll look at that in the next lesson.

Then if that object exists within my system, other parts of the system could query it to discover its values. Let’s say the object was called RichardsCar, we could ask it for its manufacturer and it will respond ‘Seat’

We can also have methods on a class. A method is a block of code that has a specific behaviour. I could have a class that has two properties - birthday and current date. We could then have a method called ‘Calculate Age’. This method would work out the person’s age using the two properties and return them to the code that sent the request.

Next Lesson ➔

Richard Bradshaw's photo
Author

Richard Bradshaw

@FriendlyTester

Software Tester, speaker and trainer at Friendly Testing. BossBoss at Ministry of Testing. Whiteboard Testing creator. Striving to improve the testing craft.

Comments