A Brief Intro to Classes and Objects
[comp1110-79] - Fabian Muehlboeck
A Brief Intro to Classes and Objects
[comp1110-79]- Fabian Muehlboeck
The core concept in object-oriented programming is that of an object. An object combines data and behaviour, though this is something we will discuss later in the course. For now, we introduce some basic ideas that you will see more of very soon.
Construction 1. Class Definitions
[comp1110-80] - Fabian Muehlboeck
Construction 1. Class Definitions
[comp1110-80]- Fabian Muehlboeck
In Java, objects are instances of classes.
A class definition is a kind of data definition, similar to what we have seen before for enums.
However, classes are much more powerful, and they are used to model pretty much everything in our programs that cannot be modeled by a simpler data type.
The core parts we will deal with for now are fields, which contain an object's data, and methods, which define an object's behaviour.
The basic definition of a class consists of the keyword class and a class name, followed by curly braces, like so:
This does define a new type Person.
To create a new instance of it (i.e. an object), you could write something simliar to what we did for arrays, using the keyword new:
This is not very useful, because the Person class above has no data or behaviour, but you can add those.
By convention, class names in Java should be nouns that start with a capital letter, and use capital letters to mark the start of any word internal to the name.
For example: LectureNote .
Construction 1.1. Fields
[comp1110-81] - Fabian Muehlboeck
Construction 1.1. Fields
[comp1110-81]- Fabian Muehlboeck
Every class can have arbitrarily many fields. At the very least, each field has a type and name, just like a local variable. There are some other properties that fields can have, which we'll see later. To define a field, you declare it just like a local variable:
You can also give the field a default value, just like a local variable definitions:
By convention, in Java, field names are camelCased.
That is, they start with a lower-case letter and otherwise contain only letters and digits,
with uppercase letters indicating the start of a new combined word, as in sumOfAllParts.
In principle, you can read and write the fields of an object similar to how you can read and write individual array elements, though the syntax is more like when you ask an array for its length:
Warning.
- Fabian Muehlboeck
Note that Warning.
- Fabian Muehlboeck
array.length has the special property that you cannot assign values to it.Example 1.2. Fields
[comp1110-82] - Fabian Muehlboeck
Example 1.2. Fields
[comp1110-82]- Fabian Muehlboeck
Say we want our Person class from earlier to have two fields: one representing the name of the person, and one representing their age.
Names would typically be represented as Strings, and in our case, we only care about completed years of age, so we use int as the type for that field.
We can then create a new Person value and write to its fields:
Warning.
- Fabian Muehlboeck
This is not how you would typically do this; we'll learn about the right way to do this later.Warning.
- Fabian Muehlboeck
Construction 1.3. Methods
[comp1110-83] - Fabian Muehlboeck
Construction 1.3. Methods
[comp1110-83]- Fabian Muehlboeck
So far, we have sometimes written code in separate functions from the main function.
But in every case so far, we had to give those functions all the arguments that they were supposed to work with.
Methods defined in a class implicitly have access to the fields defined in that class. This is because methods always need to be called on a specifc object, whose field values can then be used within the method.
So in principle, writing a method is just like writing a function, except inside a class. Within the class, you can all other methods within the class just like we called functions before.
By convention, in Java, method names are camelCased.
That is, they start with a lower-case letter and otherwise contain only letters and digits,
with uppercase letters indicating the start of a new combined word, as in sumOfAllParts.
Example 1.4. Methods
[comp1110-84] - Fabian Muehlboeck
Example 1.4. Methods
[comp1110-84]- Fabian Muehlboeck
Building on our example from Example 1.2, imagine we want to just have a way to
print a nice description for any of our basic Person objects.
To do this, we'll add a printDescription method to the class:
We can try this with the following main function:
This will print:
1.5. Have we seen Objects before?
[comp1110-85] - Fabian Muehlboeck
1.5. Have we seen Objects before?
[comp1110-85]- Fabian Muehlboeck
Yes! Every string and array is an object.
When we wrote guess.charAt(5), we were calling a method of the String class on the
particular String object referred to by guess.
Similarly, the difference between arr.length for arrays and str.length() for strings
is that the former is a field that we can access, and the latter is a method we have to call.