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".

Taxonomy Tree

A class and all of its subclasses form a taxonomy tree. The above UML diagram shows the taxonomy tree of AShape and its variants (or subclasses), namely Rectangle and Circle.

Polymorphism

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

s.getArea (); // the code for Circle.getArea () is invoked.

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

t.getArea (); // the code for Rectangle.getArea () is invoked.

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

t.getArea (); // the code for Circle.getArea () is invoked!

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. Polymorphism means a class can be represented any of its subclasses. We can think of polymorphism as viewing the taxonomy tree from the top down.

Recall OOP Programming Principle 2: Program to the abstract class (or interface). Avoid using conditional statement to check for the class type. Just let polymorphism direct all flow controls!

Inheritance

Class B inherits class A means B contains all the code of A and can automatically access all the non-private fields and methods of A. In Java, B may override any non-private method of A that is not final, and provide new behavior by redefining it with new code. B may also have additional fields and methods that A has absolutely no knowledge of. As such, B is said to be a specialization of A. Inheritance is also called the "is-a" relationship between two classes. We can think of inheritance as viewing the taxonomy tree from the bottom up.

Polymorphism and inheritance are dual concepts that always go hand in hand together. They constitutes the foundation of object-oriented programming. Good object-oriented designs effectively exploit polymorphism/inheritance to reduce code complexity and facilitate code maintenance.

Instance Variables and Instance Methods

AShape s = new Rectangle (6, 7);

AShape t = new Rectangle (3, 4);

s.getArea ();

The code for getArea () is executed and can only access the _width and _height of s. It does not know anything about the _width and _height of t.

_ width and _ height are said to be instance fields (or variables)of Rectangle.

double getArea ()is said to be an instance method 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.