5

Classes and Objects

As mentioned in the introduction, Java is an object-orientated programming language. Object-orientated programming is a paradigm that uses the concept of an object to encapsulate the data and behavior of a program. Objects are reusable modules, and can interact with one another as the program executes.

This differs from many other programming languages that use functions or procedures to encapsulate the behavior of the program: these languages are called functional-orientated and imperative languages respectively.

Object-orientated programming was already well established when Java was created, and can be traced all the way back to the 1960s. Java was notable for being a more pure object-orientated language than many of its direct competitors, however, most notably C++..

The most important concept to grasp in this chapter is the difference between a class and an object. A class acts as a template for objects: it declares the data that will be held by an object, and the methods that operate on this data. Put another way, once a class is defined, many instances of the class can be constructed, and these are called objects.

You can think of classes as molds. Once you have constructed your mold you can use it to create as many objects as you like from the mold.

Some object-orientated languages let you create objects without classes (yes, I am looking at you again JavaScript). With Java, however, you always need to start with a class.

Each object created from a class is independent from the other objects constructed from the same class, but the methods available remain the same. Before explaining the concepts of classes and objects in more detail it is worth doing so with examples.

Classes are usually thought of as the “nouns” of a program. You may remember from grammar class that nouns are used to name “things” such as people, places, ideas or animals. These are opposed to verbs, which describe actions.

When you first sit down to write a Java program you need to think about what the nouns will be. For instance, in a rental car program the nouns may be Customer, Car and Rental agreement. In a banking program the nouns may be Customer, Account and Branch.

Once you have defined the nouns, you next start thinking about the data each of these nouns needs to hold. You describe the data that will be captured by defining fields on the class. For instance, a Rental agreement class might contain fields that capture the daily rate, the date the agreement was signed, and any special charges that will be applied to the rate. The class defines the fields, while each object will be assigned specific values for these fields.

Finally you need to think about what behavior is required from the class; the behavior will be captured in the class’s methods. For instance, the Rental agreement class might have a method that calculates the total cost to the customer. In order to achieve this, methods can read and modify the data held by an object.

You will remember from the Hello World example that you declared a class called HelloWorld. Before beginning the examples in this chapter it is worth pointing out a quirk of the Hello World program. In that program you did not create an object from the HelloWorld class, yet its main method could still be invoked. This is because the method was declared as static:

public static void main(String[] arguments) {

Static methods are different from most of the methods that you will look at in this book because they can be invoked directly on the class, rather than instances of the class (its objects). This is a subject I will return to in later chapters.

A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html