|
|
1. AN OBJECT ORIENTED SOLUTION TO A PROBLEM |
We have already (if briefly) introduced the Object Oriented (OO) methodology; in this section we will embellish this introduction to give a much wider overview of the approach. Remember that in the object oriented paradigm a solution to a problem is constructed by defining a set of interacting objects and classes. (Objects are defined by what is termed a class; we say the objects "belong" to a class, or are instances of a class.) We can envisage these objects existing in what we might call an object space. The objects in this space each have a particular function and can be arranged in such a manner that they can communicate with one another (send messages) to produce a solution to a problem, i.e. the objects and classes inside this space can then be considered to produce various elements (sub-solutions) of the desired end solution. For example if we wish to determine the result of the following sum: X = ((1+2) * (3+4)) - 5 we might decide that we require three sorts of object:
To obtain the solution to the above problem these objects will have to communicate with each other (send messages). Thus the subtraction object would have to send the sum (1+2) * (3+4) to the multiplication object to carry out the desired multiplication; which in turn would have to send the sums 1+2 and 3+4 to the addition object. Objects will also have to send messages back containing appropriate results. |
For all this to happen we need an additional "control" method typically contained in what is called an application class to start the process off; in Java this method always has the name main. Figure 1 shows an object space in which are contained three objects of the form described above:
Each of these objects is defined by a class definition. Note that within this definition the operations which instances of this class can perform are expressed. In the Figure there is only one instance of each class but of course there is no reason why there cannot be many instances of the same class. The figure also includes an ApplicationClass which contains a method called main. This is a special method from which the solution process will start (there can only be one main method per application). The solution process then comprises the objects sending messages ("talking") to each other so that collectively they can produce the desired result. Note that in theory any object can "talk" to any other object, however the nature of the desired communication must be expressed in the class definition. Thus, although in theory the spherical object can talk to the cubic object, there is nothing in the class definition for the spherical objects that tells instances of this class how to do so! |
Figure 1: An object oriented solution to the X = ((1+2) * (3+4)) - 5 problem (the messaging process is indicated by the directed arcs).
2. ASSOCIATION |
The objects/classes depicted in Figure 1 clearly have a special relationship with one another in that one "type" of object makes use of the functionality provided by another "type" of object. In OO terms this relationship is called association. The term describes a group of "links" between classes. There are two different ways (in Java) in which objects of one class can make use of the functionality provided by some other class/object:
we will refer to the first as a "uses" association, and the second as a "creates" association. Which to use depends on the nature of the definition of the members in the class under consideration. The second approach requires that the object created is "contained" in the class in which it is created --- a practice known as aggregation or composition. |
Figure 2 shows the association that might exist between the classes depicted in Figure 1. Note that we have used filled arrows and labelled them with the nature of the association --- "creates" (and then presumably uses) and "uses". ![]() Figure 2: Association ("creates" and "uses" relationships) WARNING: The notation used in Figure 2 is not strictly UML, however it will suffice with respect to this introductory module. |
3. OBJECTS, CLASSES, INHERITANCE |
Thus, from the above, Object oriented programming is founded on the concept of objects --- the entities making up the solution to a problem. Objects are usually associated with a noun (i.e. a thing) in a requirements statement. Classes describe categories of object; this is why we say that a particular object is an instance of a class. Classes define the fields (attributes or data) that can be associated with an object, and the methods (operations) that can be applied to those fields. Collectively the fields and methods belonging to a class are referred to as the members (features) of that class. Classes are often arranged in a hierarchy whereby sub-classes (child classes) inherit features from super-classes (parent classes). The advantage is that we do not constantly have to redefine members which have already been defined previously. Inheritance is an important feature of OOP, but note that inheritance is not the same as association (the realtionships are different). An example class hierarchy is presented in Figure 3. Note that:
Where a class inherits from another class anywhere in the hierarchy, an instance of any sub-class in the hierarchy is also a legal instance of all its super-classes and thus has (at least in principle) all the fields and methods associated with the super classes available to it. |
![]() Figure 3: Class diagram demonstrating a "family tree" class hierarchy. Thus in Figure 3 an instance of the class Child is also an instance of the class Parent and the class GrandParent (but not the classes ParentSibling and ChildSibling). We say that the class child extends the class Parent which in turn extends the class GrandParent. Alternatively we sometimes say that a certain sub-class is derived from a super-class. Three categories of membership are identified:
Note that, since one of the principals of OOP is data hiding, field members are normally categorised as private members of a class. |
4. CONSTRUCTORS |
To create an instance of a class (i.e. to create an object) we use a special method called a constructor, we say that a constructor creates an instance of a class. When this happens access to the methods defined for the class and a completely new set of fields (of the form defined for the class) is "bundled" up with the instance identifier. Consequently we refer to instance fields and methods. Sometimes, given a particular application, we wish to include members in a class for which we do not wish there to be a unique copy for each instance --- such members are called class members or static members; thus in this case we refer to class fields and methods as opposed to instance fields and methods. The term static is used to indicate that such members cannot be "copied" to form part of an object. Figure 4 gives a class diagram of a single class called grandParent (taken from Figure 3) which has two fields and one method which is a constructor. ![]() Figure 4: Example Class containing a class field and an instance field. Note that:
|
If, given some application, we now want to create instances of this class, say mary, we would invoke the GrandParent constructor as follows: GrandParent mary = new GrandParent(); The keyword new indicates that a new instance of a class is to be created; when this happens appropriate storage for a completely new set of instance fields members, i.e. the field age in the example class (but not the class field averageAge), is set aside for the newly created object. The area within primary memory where the required storage is created is known as the heap. Note that we can have more than one constructor associated with a particular class. Also a constructor may have one or more formal parameters (or arguments) associated with it --- more on this later. 4.1 Garbage collectionOnce an object has been created the memory space for this object is "freed-up" as soon as the object is no longer referred to in the program through a process known as garbage collection. The Java garbage collector runs in the back ground when executing a Java program and tracks objects. When ever an object is no longer referenced the garbage collector removes it from storage (although it might defer doing so till an appropriate moment in the execution process). This ensures efficient use of available storage space. |
5. FIELDS |
A field describes an attribute/data item associated with a class. For example the GrandParent class given in Figure 4 has a class field called averageAge and an instance field called age. From within a class the fields belonging to that class can be accessed directly by referring to them by their name alone (regardless of whether they are private, public or protected members). From outside the class a field can only be accessed if it is a public class member using either:
|
We use the dot operator to do this (sometimes referred to as the membership operator). In the first case this is prefixed by an instance identifier (name) and in the second case by a class identifier. Thus: mary.age or Grandparent.averageAge |
6. METHODS |
A method is a named sequence of instructions called statements. We can identify four types of method:
where instance methods are associated with a particular instance of a class and class methods with an entire class. Figure 5 shows a redefinition of the GrandParent class used earlier (Figures 1) such that the class includes an instance method outputAge, and a class method outputAverageAge. ![]() Figure 5: Example Class containing a class method and an instance method. 6.1 OverloadingIt is possible to overload method names; i.e. to have two or more methods with the same name in a single class. However, they must be differentiated by the number and nature of their arguments.
|
One common use of overloading is to have several different constructor methods for the same class. 6.2 Method CallsAs with fields, from within their class, methods can be referred to by their name alone, but when used from outside the class the method must be a public class member and referred to with either:
Again we use the dot operator to do this. In the first case this is prefixed by a class identifier (name) and in the second case by an instance identifier. GrandParent.outputAverage(); or mary.outputAge(); The formal parameters (the arguments for the operation) are optional. When we reference a method in this manner we are causing it to execute with respect to the class or instance that it is linked to. The above is therefore referred to as a method call. The object with which a method is invoked is sometimes referred to as the receiving object or the receiver (in that it may "receive" information returned by the method). |
7. SUMMARY |
|
|
8. ANATOMY OF A JAVA CLASS DEFINITION |
A Java program consists of a set of one or more classes many of which will be related to one another in some form of hierarchy; however, not every class need be related to every other and the kinds of relationship may vary. In Table 1 a framework for a Java class definition is presented.
Table 1: Framework for a Java class The bold text in Table 1 indicates reserved words that have a special meaning. When writing your own programs it is of course not necessary to emphasise different words in this manner. Notes with respect to the framework presented in Table 1:
|
import java.lang.* ;The .* indicates that we wish to use all the classes defined in this package. Instead we could have define particular classes that we wished to use, e.g. import java.lang. System; (Note that the java.lang package is included in every Java program automatically without actually requiring an import statement). |
9. ANATOMY OF A JAVA METHOD DEFINITION |
< MODIFIERS > RETURN_TYPE METHOD_NAME( < ARGUMENT_LIST > ) { < LOCAL DATA DEFINITIONS AND CODE STATEMENTS > } |
Table 2: Framework for a Java method
Table 2 gives a similar frame work for a Java method to that given in Table 1 for a Java class. Note that:
|
|
10. OUTPUT |
In this Section we will describe the mechanisms provided by Java to achieve output. We have seen that for a program to do anything it must receive some input data via an input stream and produce some output data to be sent to the output stream. The input may simply be a signal to run the program (with no actual input data), but it would be pointless to write a program which did not produce any output. Thus the simplest Java program we can write must comprise a single output statement. We have noted that traditionally the output from a first program written when learning a new programming language comprises the phrase "Hello World!" (we will abide by this tradition). To facilitate output Java provides a number of classes. One of these is called PrintStream. This class contains (amongst other things) the methods print, println and printf. The first takes one formal parameter which it outputs to an output stream (e.g. the screen, secondary storage, etc.). The second is used in an identical manner except that it automatically includes a new line character at the end of the output. Note that in both cases the formal parameter must be a sequence of one or more characters. Such a sequence is called a string and is indicated by enclosing the characters with double quotes. Strings are discussed in further detail in 8.1 below. The printf method allows formatted print and will be discussed inm more detail later. |
A class diagram indicating the relationship the PrintStream class has with some other significant classes, at least in the context of output, is given in Figure 6. Both print and println are instance methods and therefore require an appropriate object of the type PrintStream. In addition, to use either method, we must tell Java which output stream we wish to direct the output to. Java provides us with a special "screen output" object to facilitate output to your computer screen (called out) which is contained within the class System. Because out is an instance of the PrintStream class it carries with it all the paraphernalia associated with that class, i.e. access to the methods print and println and so on. The class System is contained in the package java.lang which (remember) is always compiled into every Java program.
Thus if we wish to output Hello World! to the screen we might include the following statement in a piece of Java code: System.out.print("Hello World!"); or:
System.out.println("Hello World Again!"); Note the ';' character, every Java program statement must end with a ';' character. Note also that println when called without any arguments, i.e. println(), will simply cause a "carriage return" (newline character) to be passed to the output stream without any other output. |
Figure 6: Class diagram showing API classes associated with simple screen output.
Any sequence of characters enclosed in quotes, such as "Hello World" is called a string literal or simply a string. If we wish to include a quote character (") within a string we must prefix it with a backslash character (\). This is called the escape character. For example: System.out.print("Hello \"Frans\"."); would produce the output Hello "Frans".. Without the escape characters the Java compiler would recognise this as two strings Hello and ., separated by the "operator" Frans; this is bound to confuse the compiler! Combinations of back slashes and special characters such as the quote character are sometimes referred to as escape sequences. The concept of escape sequences is common to many languages, for example C and C++. Further examples of escape sequences include \n which indicates a carriage return and \t which indicates a tabulation ("tab") marker. Thus instead of writing System.out.println("Hello World Again") we could have written: |
System.out.print("Hello World Again\n"); One further comment on strings. Sometimes, when programming, a string gets too long to fit on a single line in the editor window. If we simply press the return key and continue typing in the next line we will have introduced a newline control character into the string which the editor can understand but the Java compiler would have problems with. One option would be to use a sequence of print methods. Alternatively we can concatenate a sequence of strings together using the '+' concatenation operator. For example: System.out.print("A very long string " + "literal that, unfortunately is " + "so wide that it disappears of " + "the end of the screen, however " + "we can avoid this undesirable " + "feature by splitting the string " + "up into a sequence of window " + "wide sub-strings and link them " + "together using the Java \"\+\" " + "concatenation operator.\n"); Note the use of the escape character in \"\+\". |
11. FIRST APPLICATION PROGRAM ("HELLO WORLD!") |
We are now in a position to put all of the above together and write our first Java application program (Table 3).
Table 3: Hello world Java application program Once compiled we can run this code using the java interpreter by typing: java HelloWorld This will produce: Hello World! Note that:
|
/* A block of comments. Many programmers consider this to be much more elegant than a sequence of single line comments. */This approach is also a feature of languages such as C (and C++). It is good programming practice to include comments in your code, thus in the above case we commence with the name of the program (class), the author, the date when it was written, and where it was written. This information is all extremely useful when it comes to maintaining the code. |
As far as the Java compiler is concerned layout in terms of new lines and spaces (with the exception of strings) does not matter provided that the source code is syntactically correct. However, from a software engineering perspective a well laid out program is much easier to read/understand than one that is not well laid out. The following version of the above code presented in Table 3 will compile and execute, although it is very difficult to "read": |
class HelloWorld{public static void main(String argv[]){System. out.println("Hello World ");}} It is therefore a good idea to write programs neatly using appropriate indenting, comments and "white space" so as to enhance understandability. "If something looks good it probably is good!" |
Created and maintained by Frans Coenen. Last updated 10 February 2015