Your Feedback will be greatly appreciated:

Please e-mail us your feedback (via anonymous e-mail if so desired)! We need your help to make this the best little computer science course in Texas!

What is null?

AShape s; // s is assigned the value null (similar to nil in Scheme).

When a variable of some class is declared without any initial instantiation, it is assigned the value null.

null represents "non-existence". Assign null to a variable of some class type only in the case you want to express "non-existence".

Polymorphism

AShape s = new Circle (2.7); // OK.

AShape t = new Rectangle (3, 4); // OK

t = s; //OK, the old Rectangle is gone (garbage collected)!

Circle u = new Rectangle (5, 6); // NO NO!

A variable of class AShape can be assigned any instance of subclasses of AShape at any time in a program. AShape is said to be polymorphic. In general, a variable of a superclass can be assigned an instance of any of its subclasses, but not the other way around.

Instance Variables and Instance Methods

AShape s = new Rectangle (6, 7);

AShape t = new Rectangle (3, 4);

s.dArea ();

The code for dArea is executed and can only access the _dWith and _dHeight of s. It does not know anything about the the _dWith and _dHeight of t.

_dWith and _dHeight are said to be instance fields (or variables)of Rectangle.

double dArea () is said to be an instance method of s of Rectangle. It can only be called on an existing instance of a class.

 

static Variables and static Methods

Suppose we want to keep track of how many Rectangles are being created during the course of our program. What do we need? Answer: a field that is unique and global to all instances of Rectangle and that can be accessed by all methods in Rectangle. In Java we use the key word static.

class Rectangle extends AShape

{

private static int _iCOUNT; // Initial value is 0.

// ..other stuffs…

public Rectangle (double width, double height)

{

_iCOUNT++;

// each time this constructor is called, _iCOUNT is incremented // by 1.

// other stuffs…

}

}

static methods cannot access instance fields, and can only access static fields. It can be called before any instantiation of the class.