Lecture 1 (1/28): Object-Oriented programming in Java

Classes and Subclasses

Read: Dale, Joyce, and Weems sections 1.3-1.6


Each object O of this class C has, actually, its own copies of the fields, and, conceptually, its own copy of the methods.
To access the F field of O write O.F
To call the M method of O write O.M()
Inside the class definition of C itself, you can refer to fields and methods of the object under discussion as just F and M For example in Rectangle, in method getXSpan we refer to field xSpan This means, ``when calling O.getXSpan(), use O.xSpan.'' Sometimes it is necessary to use the keyword this so that the compiler knows what you mean.

Information hiding


Note the two calls to the constructors and the call to the method.


Defines LocatedRect as a subclass of Rectangle. Note:



If you don't define any constructor for a class, then Java automatically defines a default constructor which creates the object and sets all the fields to be default values (0 for numeric fields, null for object fields). If you do define a constructor with arguments for a class, then Java will not automatically define a default constructor.

Objects and References


A variable of class C is a reference to an object of class C. When you execute Rect3 = Rect1, what happens is that Rect3 becomes a reference to the same object. So, changing a field of that object changes it in both Rect1 and Rect3 because they are the same thing.

When we execute Rect3 = Rect2 what happens is that Rect3 now references the same object as Rect2.

Calling new Rectangle creates a new object; it is the only way to create a new object.

Objects that reference other objects



Parameter Passing


Parameter passing in Java is always call-by-value. The value of the actual parameter in the caller is copied into the formal parameter in the callee, which is a different variable. Reassigning the formal parameter has no effect on the actual parameter. For instance, when main calls g(I), the current value 1 of the actual parameter I is copied into the formal parameter C. Reassigning C to be 10 has no effect on I .

The same is true of objects passed as parameters. However, the values of the variables are not the actual objects; they are references to the object. So when main calls f(W,X) the value of W is copied into R and the value of X is copied into S. So R points to the same object as W and S points to the same object as X Reassigning S does not affect the value of X but changing the fields of the object referenced by R does change the fields of the object referenced by W, since they are the same object.



The class TestOverload has two methods called show one for objects of class A and one for objects of class B. This is called overloading the method name. You can also overload operators; e.g. + means integer addition, floating point addition, string concatenation, and so on. Note that you are allowed to define a class with neither data fields nor methods. As far as I know, this is only useful for demonstrating language features; I don't think you ever want to do it in practice.


In TestOverload1 things are trickier because each of the forms of show has two arguments of different classes.