Chapter:Objects - Using

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.

Purpose

The purpose of this chapter is to teach the students about objects by focusing on how objects are used in Java code, and what you can do with objects.

The pedagogical idea behind all this is that we first introduce the idea of objects - they exist and have state and behavior. Next we show code to create objects (but not everything you can do with them, once they are created). After that, we admit that the objects usually are described in something called classes, without going into what classes look like in code. And now, in this chapter, we take the next step - put the objects to work!

So, first introduce, then create, and after that use is the idea here. Or, in other words, taking things slow. We simply wanted to avoid exposing the students to too much at the same time.

What we mean by "using" objects, is divided into four lectures:

  • Using objects - short introduction to sending a message to an object, i.e. calling an instance method
  • Invoking methods - more about calling instance methods, also discusses that many objects of the same type each have individual behavior, even if the message is the same - we can have many objects of the same type and we communicate with them via references
  • Getting information from objects - Some methods don't have side effects, but rather return a value, while others "just do something" without giving any value back to the caller - void methods are introduced
  • Objects using objects - here we show that it is not uncommon that an object calls a method on another object. An instance method, for instance, can accept an argument of type "reference to some object". This is (an example of) how an object can use another object.

Goals

The goal of this chapter is that the four lectures described above gives the student a better understanding of what we can do with objects, e.g. sending methods (calling instance methods) and that two objects can respond to the same message (both have the same instance method) but that the result of calling an instance method depends on the state of the object. And finally, another goal is that the student also understands that references to objects can be passed as arguments to an instance method, so that the object receiving the reference, in turn, can call other methods on via that reference.

Instructions to the teacher

Common problems related to this chapter

We've seen that students struggle with understanding the difference between a void method and a method with a return type (void is not considered a type in Java). We think that part of the confusion comes from examples we've seen where a method prints some string to standard out, which the students interpret as a return value.

One way to avoid this particular source of confusion, is to teach students that printing means communicating with the user or surrounding system, not communicating with other pieces of code in the same program. If a method should print something, make it a void method. If the method should calculate or expose some value, don't make it also print something. It is better to do the printing in e.g. the main method after the method has returned the value. This should make it more clear that the method calculates and gives back a value, and printing is something else.

In particular, avoid writing exercises on the form: Write a method which takes two integers as arguments and prints the sum. It is, in our experience, much less confusing for the student to instead use this form: Write a program which calls a method which can accept two integers as arguments and return the result. The program should save the returned value and print it out.

Another problem when teaching Java, is that the students tend to get confused by the fact that it is unclear (initially) what code constitutes a program in the sense that it "does something", and what code is general code which can be re-used.

Sometimes, we talk about classes which declares methods and variables, and other times we talk about a program using such classes. It is still early in the course material (heck, we haven't taught how to write classes yet!), but we'd like to point this out already. A common source of confusion is literature and course materials which focus on "programming" in the sense "doing something" like solving a problem of a mathematical nature. While there's nothing wrong with such programs, the problem is that such literature and course material for some reason tend to focus on problems small enough to be solved in one single source code file, with one single class and a main method which calls a few other static methods in the same class. This, if introduced early, might make the transition to more object oriented problems and more object oriented solutions hard for the students.

Our material could be classified as "objects first" or "objects early" type of material. We believe that it is better to show how most object oriented problems are solved - using objects which solve specialized tasks and represent specific entities "in the real world" that the program models. We avoid self-contained classes with only static methods as far as possible, exactly for the reasons stated above. We simply think that the cons outnumber the pros in that such programs are not very representative of Java programs "in the real world". We further believe that problems which have an obvious non-object oriented solution could be solved with other languages than Java.

Using objects

Now that we've seen how to create objects, we'll look a little at what we can do with the objects.

The point in this section is to show you how you use objects by sending messages to them. Sending messages is another way of saying "calling an instance method of an object" or "invoking an instance method of an object".

We've learned that our programs deal a lot with objects. We've seen a few ways of creating objects but we haven't really interacted with the objects. The typical way of using objects in a program is to call methods on the objects, which is like asking the objects to perform some task. Sometimes they silently perform some task which affects other objects or things outside the program (perhaps a web page is loaded and displayed to the user, perhaps a file is saved on the hard drive). Other times, they give as feedback by sending information back to the caller of the method. In later sections, we will also see that we can send references to other objects to an object if they need to work together to perform some task (we've actually seen a version of that, such as when we send a reference to a String to the println method of the object referred to by System.out - the System.out object needs a reference to a String object in order to print it to the standard out stream).

We'll see a few new classes from which we create objects and we'll see that we can ask certain classes to give us references to objects. And we'll see the syntax for calling instance methods via object references.

An instance method is a method that is declared to operate on the state of a particular object (remember an object can also be called an instance of a class). Instance methods are declared in the classes describing objects of the same type and they are declared without the static keyword (that we've seen in the main method - the main method is not an instance method, it is said to be a class method or simply "static method").

The point of instance methods is that all objects are modeled by some class where their data (state) is described, their constructors are described and their behavior is described. So objects of the same class are very similar in structure and behavior. But objects are kind of individuals, so their state (internal data) may differ. So when we want a particular object of some class to do something based on its specific state, we use an instance method. We call the instance method typically using a reference variable referring to the object. The method now executes and it has access to the state (stored in instance variables) of this particular object.

This is an example of an instance method for calculating the age of a person. We can pretend that it is located in the Person class and that each person has a LocalDate object with the birth date of the person:

  public int age() {
    return Period.between(birthDate, LocalDate.now()).getYears();
  }

If we have a Person object and a reference variable to this object, called aPerson, then we'd get the age of the person by calling int age = aPerson.age();.

If we have a class describing users in a computer system we can create several User objects from some constructor in the User class. Even though they are all instances of the User class, they will probably have different state. The state can be a user name (stored in an instance variable of type "reference to String" for instance), an email address and a password for instance. The behavior can be for instance that users may change their password. If we send the message to change password for one User object, we don't want the password to be changed for every User object in the system. This is why instance methods are really handy. An instance method for changing the password would only operate on one particular object and we call it for that object using a reference to it.

Before we dig into creating our own classes for creating objects, we'll use some more of the Classes that are included in the Java standard library (the Java API). We'll look at Property objects (that hold settings as key-value pairs), and we'll look at a type of List class for creating list objects that can hold for instance a list of String references, but behave more flexible than the array we've seen previously.

Chapters about Objects

Here are the chapters about objects in this book:

Videos

English videos

Swedish videos

Watch the English videos right here

If you don't want to head on to Vimeo, you can watch the (English) videos right here on our page.

Links

Further reading

Where to go next

Next page has the exercises for this chapter: Objects - Using - Exercises

« PreviousBook TOCNext »