Introduction to Classes

The following applet demonstrates the interaction between classes / objects in Java.

Class "Train" describes a simple toy train. Each of its objects keeps track of the color of the train and the number of its cars. Methods are available to modify these variables. The Java code for class "Train" is displayed. Methods that are being called are highlighted.

Class "RailRoad" has a main method in which objects of class "Train" are to be created and modified. In the applet the user gets the opportunity to create and modify objects by simple mouse clicks. These mouse clicks are translated into corresponding Java commands and displayed.

Method calls (also referred to as messages) are commonly qualified by parameters. In this applet the value of any such parameter can be chosen from a list of available ones before pressing the button that will result in the method call being issued. The same applies to constructors.

In object oriented programming languages method calls (messages) also have to specify the object on which they operate (recipient of the message). In this applet the object (recipient) is assumed to be the most recently created train unless otherwise specified ("Select the train:").

Is the displayed code identical to the code used in the applet?

Not quite: Class "Train", as it is displayed, does not include the functionality for drawing a train. The class used in the applet does. Also: In order to run the program in a browser window it has to be an applet while for simplicity the code for a Java application is displayed. (The applet code itself contains concepts that go beyond the scope of this simple demonstration.)