Creating your first class

In this section we will start to develop a representation of a generic crop that could be used to simulate its growth and development depending on the amount of light and water it receives over a particular period of time.

Keep in mind that this is a simple simulation and the intention is to introduce key object-oriented programming concepts and the idea of simulation rather than producing an accurate representation of an actual crop.

The crop class

Our crop is going to be a simple one, it will require a certain amount of light and water and if it receives sufficient amounts it will grow. As it grows it will develop from a seed to a mature crop.

In the previous section you were introduced to attributes and methods, we can represent these for the crop on a class diagram.

Crop Class Diagram

Creating the crop class

The video below demonstrates how to create the class from the above diagram.

Task 1

Use the above video to help you create the crop class.

Instantiating the crop objects

When we create a class we create a blueprint that can be used to create objects of that class. We can the process of creating an object from a class instantiation.

Once you have written your class definition you can use it over and over again to create as many different objects of that class as you like.

The video below demonstrates how to instantiate crop objects from your class.

Task 2

Use the above video to help you create an instance of the crop class.


In the above videos we indicated that some of the crop attributes should be considered private by placing an underscore before the name of the attribute. This indicates to other programmers that they should not access these attributes directly. Instead they must access your class through its public interface, that is the methods that you make available in your class.

We call the process of hiding information in this way encapsulation and it is demonstrated in the animation below:

An explanation of encapsulation

Understanding the television example

If we were to produce a class to represent the television from the animation it may have an attribute to represent the frequency it is tuned to and a method that enables you to change the channel.

We don't want people to change the frequency directly as they would need to understand how to tune the frequency correctly and most people do not know how to do this. They may not be able to tune it at all and therefore the television would be worthless to them or they may tune it incorrectly and damage the television in some way. Neither of these scenarios is acceptable.

Therefore, by providing a method that can be used to change the channel we can ensure that the user can switch stations in a way they can easily understand and that they can not cause damage to the television.

Other situations

In other situations we may not be so concerned about whether another programmer can understand our code but rather that they use it in the way it was designed.

If another programmer relies on one of your private attributes in their program and if then you remove this attribute in a future version of your code their program will stop working. By sticking to the public interface another programmer is not relying on implementation details that may change and therefore they can ensure that their program will continue to work as long as you make the same methods available to them.

Public interface

In the next section we will start to develop the public interface to our class - its methods.