two new features, we have removed the two problems with class Shape .
• Subclass . A subclass is a class that extends another class, called its superclass .
The subclass inherits all the components of the superclass, i.e. each instance of
the subclass contains the instance components of the superclass. The subclass
can define its own components and override (redefine) the inherited methods.
• Class invariant . The values of the fields of a subclass generally have to satis-
fy certain constraints. It is a good idea to write these constraints as a comment at
the beginning of the class body, just before the declarations of the fields.
• Subclass constructor . A constructor in a subclass should begin by calling a
constructor of the superclass to initialize the inherited fields. In the constructor
call, use keyword super instead of the superclass name.
• Overriding an overriding method . If a method m is overridden, a call m(… )
calls the overriding method. Use the notation super .m(…) to call the inherited
• Casting . One can cast an expression to a class C using the prefix operator (C) .
• Apparent and real class-types . The apparent class of a class expression e is
syntactically determined from the class-types of its operands. A component ref-
erence e.v or e.m(…) is legal only if variable v or method m(…) is available in
the apparent class of e . The real class of e is the class of the folder whose name
is currently in e ; the real class can change whenever e is assigned a value. The
variable or method actually referenced by e.v or e.m(…) depends on the real
class of e , and not on the apparent class of e .
• Operator instanceof . Expression e instanceof C is true if and only if the class
of folder e is C or a subclass of C .
• Class Object . Object is the superclass of all classes that do not explicitly
extend a class. Object contains instance functions toString and equals , which
are often overridden in subclasses. Boolean function equals should be an equiv-
• Access mechanisms . The four access mechanisms public , protected , package ,
and private provide increasing restrictions on access of components. If you are
using only the default package, there is no difference between the first three.
• Object-oriented design . The is-a relation provides insight into OO design with
subclasses. If a problem-domain entity x is a y (e.g. an undergrad is a student), it
may make sense for the class of x to be a subclass of the class of y . Common
behavior of several subclasses is moved to their superclass as much as possible.
• Abstract classes and methods . An abstract class cannot be instantiated; an