In this segment, I will review the Java statements and primitives that relate explicitly to Object Oriented Programming. I need to re-enforce Java s commitment to OOP. Unlike C++, there is no way to build a Java program that does not follow the OOP principles. The Java program itself is an object. George Blankenship 1
The key concepts of OOP are the creation of a object definition which is normally called a class. The class is the fundamental component of OOP programming; all objects trace their roots, their definition to a class. The program has no free standing constructs outside the class. The result of using the class to define all entities in the program is that the programmer is able to create new data types based upon the class paradigm. The class is an abstract definition of an object that allows objects to treated a data items with individual characteristics and behavior. Java, as was said earlier, is completely object oriented. George Blankenship 2
There are a number of terms that are paired. They will be used throughout the rest of the discussion and the next few slides are devoted to explaining the terms and their relationships. Class and object are intrinsically related, as I have said earlier. Once an object is created, the object may have many handles as reference points. Some of the pairings are language unique and not present in Java such as initialization block and destructor; however, the terms need to be clarified. Finally, there are modifies that define the scope of the name, whether it is a Class, data item (object) or method (behavior). George Blankenship 3
The Class is defined in the code; the object does not exist in the code. The code will indicate that an object is to be created (instantiated), but instantiation is performed during the execution of the program. In Java, an object is instantiated by use of the new operator. The Java snippet new ClassName() would instantiate an object based upon the Class ClassName. The Class blueprint defines the attributes and behavior of the object. The attributes of a class are the internal data items of the object. Each attribute is either a fundamental data item such as an integer (int) or an object. The attributes define the state of the object; that is, give the object uniqueness. The behavior of a Class is defined by the methods defined in the source; the methods would be called functions in other languages such as C. The Java snippet void computethenationaldebt(date year, Value grossdomesticproduct, Value nationalbonds){} is an example of the definition of a method with the name computethenationaldebt and a void body. When the method is invoked, it expects three parameters: year, grossdomesticproduct, and nationalbonds. The three parameters are objects; the first is of the class Date and the second two are of the class Value. A method is invoked much like a function in other languages and may return a single object as a result. Generally, when discussing objects, the invocation of a method is sometimes done describing the invocation as passing the object a message. The invocation is accomplished by using the object handle (object name) with the method name; in the example, if the name of the object is name, then the Java snippet name. computethenationaldebt(year, gdp, bonds) would invoke the method with the George Blankenship 4
parameters: year, gdp, and bonds. George Blankenship 4
The new operator creates an object. The object does not have an known immutable identity; it is known by an associated handle. In the prior example the Class name was ClassName. The Java statement ClassName newobject = new ClassName(); would create an object with handle newobject. The handle for the object is not the object; the handle may be associated with another object from the same class. The same object might have multiple handles. The equality test (==) tests that the two sides are the same object, not just the same attributes. If one needs to know whether two handles are references to the same object, or aliases for the same object, the equality operator is used. If one wishes to know whether the attributes are the same, one must use the equals method from the class. The statement zack == Zack does not test the contents of the object zack; it tests that the handle zack is also the handle for the static compiler generated string Zack. There is no string comparison performed; the equals method would perform a string comparison between the two distinct objects. George Blankenship 5
In these two examples, the references example creates an object with two handles (X and Y); the objects example creates two unique objects (X and Y) that start with the same attributes. The two examples demonstrate the difference between a single object with two handles and two unique objects. The modifying example clarifies the difference. The references example is used to demonstrate that the two handles really reference the same object. When X is changed, so is Y. The two unique objects do not have the same behavior; if X is changed, Y retains the initial value. George Blankenship 6
George Blankenship 7
George Blankenship 8
George Blankenship 9
George Blankenship 10
George Blankenship 11
George Blankenship 12
George Blankenship 13
George Blankenship 14
George Blankenship 15
George Blankenship 16
George Blankenship 17
George Blankenship 18
George Blankenship 19
George Blankenship 20
George Blankenship 21
George Blankenship 22
George Blankenship 23
George Blankenship 24
George Blankenship 25
George Blankenship 26