Java classes typically describe some real-world object, such as a customer, a purchase order, or a vehicle, as described above. These classes use instance variables, such as color, to define their properties. In addition to these properties or characteristics, most classes also have defined behaviors. These behaviors or actions are known as Methods. The Vehicle class shown in Figure 26.15 expands the previous version by adding the Paint method. This method changes the color of the instance.
Figure 26.15: Java methods.
Figure 26.16 shows the new Code for the Buycar application. This time, it creates a single instance of the Vehicle class, prints its original color, uses the Paint method to change the vehicles color, and then prints the new color.
Figure 26.16: Java methods.
The following command invokes the Buycar application:
The output is as follows:
Original Color = Silver New Color = Gold