Chapter 7 Notes (Object-Oriented Programming)
(Deitel "Java How to Program" 2E)
I. Inheritance
A. A form of software reusabilty in which new classes are created from
existing classes by absorbing their attributes and behaviors and
embellishing these with capabilities the new classes require,
thus saving time in program development by encouraging the reuse
of proven and debugged high-quality software, and reducing
problems after a system goes operational.
B. When creating a new class, instead of writing completely new
instance variables and instance methods, the programmer can
designate that the new class is to 'inherit' the instance
variables and instance methods of a previously defined 'superclass'.
The new class is referred to as a 'subclass', which in turn can
become a 'superclass' for some future subclass.
C. Java does not support multiple inheritance, as C++ does, but instead
uses 'interfaces'.
D. A subclass normally adds instance variables and instance methods of
its own, so a subclass is usually larger than its superclass.
E. A subclass is also more specific than its superclass, representing
a smaller group of objects (objects which meet a more specific
set of requirements from the more general set of requirements
of the superclass).
F. The real strength of inheritance comes from the ability to define in
the subclass additions to, or replacements for, the features
inherited from the superclass.
G. A class can also inherit from abundant class libraries.
H. Every object of a subclass is also an object of that subclass's
superclass, but a superclass is not an object of it's subclasses.
I. Subclass methods and methods of other classes in the same package or
subdirectory can directly access public, package, and 'protected'
members in the associated superclasses.
J. Superclass members that should not be directly accessible to a sub-
class via inheritance are declared 'private' in the superclass.
K. A subclass can effect state changes in superclass 'private' members
only through 'public', 'protected', and package access methods
provided in the superclass.
L. A subclass can inherit methods that it does not need or are not
appropriate, from it's superclass. In such a case, the subclass
can 'override' that method by redefining it in that subclass.
M. Inheritance is used to represent the relationship between classes
X and Y, where Y 'is-a' X, or Y 'is-a-specific-example-of' X, such
as a rectangle is a quadrilateral.
N. Composition, on the other hand, is used to represent the relationship
between classes X and Y, where Y 'has-a' X, such as an employee has a
birthdate.
II. Superclasses and Subclasses
A. Fig. 7.1, p. 349
B. Fig. 7.2, p. 239
III. 'protected' Members
A. 'public' members of a superclass are accessible by all methods in
the program.
B. 'private' members of a superclass are accessible only by methods of
the superclass.
C. 'protected' members of a superclass are accessible only by:
1. methods of the superclass
2. subclasses and methods of other classes in the same package or
subdirectory
D. Subclass methods can normally refer to 'public' and 'protected'
members of the superclass by using the member names.
E. When a subclass method 'overrides' a superclass method, the super-
class method may be accessed from the subclass by preceding the
superclass method name with 'super.'.
IV. Relationship between Superclass Objects and Subclass Objects
A. An object of a subclass can be treated as an object of its corres-
ponding superclass, but a superclass object is not automatically
a subclass object.
B. The programmer may, however, use an explicit cast to convert a
superclass reference to a subclass reference, IF the superclass
reference is actually referencing a subclass object; otherwise,
Java will throw a 'ClassCastException' at compile time(syntax error).
1. Fig. 7.4, pp. 351-354
V. Using Constructors and Finalizers in Subclasses
A. When an object of a subclass is instantiated, the superclass's con-
structor should be called to do any necessary initialization of the
superclass instance variables of the subclass object.
B. An explicit call to the superclass constructor(via the 'super' ref-
erence) can be provided as the first statement in the constructor
of the subclass; otherwise, the subclass constructor will call the
superclass default or no-argument constructor implicitly. BE SURE
that the superclass does have such a constructor!
C. Superclass constructors are not 'inherited' by subclasses, but the
subclass can call the superclass constructor via the 'super' ref-
erence.
1. Fig. 7.5, pp. 357-360
D. finalize() should always be defined as 'protected' so subclasses have
access to the method, but other non-sub-classes do not.
E. finalize() should always include throw the exception 'Throwable' as
in 'public void finalize() throws Throwable' so as to completely
override the finalize() method in the Object superclass, which also
'throws Throwable'.
F. When an object is set to 'null', Java marks the memory occupied by
that object for (eventual) garbage collection.
G. Java guarantees that before the garbage collector runs to reclaim
the space for each 'nullified' object', the finalize() method for
each object will be called.
H. The garbage collector is a low-priority thread that runs atuomatically
whenever processor time is available.
I. The garbage collector can also be explicitly called by
System.gc();
J. Java does not guarantee the order in which objects will be garbage
collected nor the order in which finalizers will be run.
VII. Composition vs. Inheritance
A. Fig. 7.6, pp 364-365
B. Fig. 7.7, pp. 365-367
C. Fig. 7.8, pp. 368-369
VIII. Polymorphism
A. Polymorphism enables:
1. design and implementation of systems that are now easily
extensible
2. writing of generic functions to process objects of all existing
classes in a hierarchy
3. adding of classes during program development with little or no
modification to the generic part of the program, as long as
those classes are part of the hierarchy being processed
generically
IX. Type Fields and 'switch' Statements
A. A 'switch' statement could have an entry case for each object,
based on the object type, but this approach has many problems:
1. The progreammer might forget to make such a type test when one
is warranted
2. The programmer may forget to test all possible cases in the
'switch'
3. If a 'switch'-based system is modified by adding new types, the
programmer might forget to insert the new cases in existing
'switch' statements
4. Every addition or deletion of a class demands that every 'switch'
statement in the system be modified, and tracking these down can
be time consuming and error prone.
B. Polymorphic programming can eliminate the need for 'switch' logic,
by allowing the programmer to use Java's polymorphism mechanism
to perform the equivalent logic automatically.
X. Dynamic Method Binding
A. If Circle, Triangle, Rectangle, Square, etc., are all derived from
Shape, and each class in this hierarchy has a draw() method, it
is desirable to be able to treat all of these shapes generically
as objects of the superclass Shape. Then, to draw any shape, we
can call draw() in Shape and let the PROGRAM determine dynamically
at execution time which subclass draw() method to use.
B. To enable this kind of behavior, declare draw() in the superclass,
and override draw() in each of the subclasses. Each sublcass's
draw() should contain the required code to 'draw' the particular
shape associated with that subclass.
XI. 'final' Methods and Classes
A. 'final' variables cannot be modified once declared and must be
initialized when declared.
B. 'final' methods cannot be overridden in any subclass.
C. 'static' and 'private' methods are implicitly 'final'.
D. Because a final method's definition can never change, the compilier
can optimize the program by removing CALLS to final methods and
replacing them with the expanded code of their definitions (inlining)
E. A 'final' class cannot be a superclass, and all methods in a 'final'
class are implicitly 'final'.
XII. Abstract Superclasses and Concrete Classes
A. No object of an abstract class can be instantiated.
B. The sole purpose of an abstract class is to provide an appropriate
superclass from which other classes may inherit interface and/or
implementation.
C. Classes from which objects can be instantiated are called 'concrete'
classes.
D. A class is made abstract by declaring it with the keyword 'abstract';
otherwise, the class is 'concrete'.
E. A hierarchy does not need to contain any 'abstract' classes, or it
may contain more than one level of 'abstract' classes.
XIII. Polymorphism Examples
XIV. Case Study: A Payroll System Using Polymorphism
A. Fig. 7.9, pp. 375-381
XV. New Classes and Dynamic Binding
A. New classes are accomodated by dynamic method binding ('late' binding)
B. An object's type need not be known at compile time for a polymorphic
call to be compiled. At execution time, the call will be matched
with the method of the called object.
For instance, the draw() method call remains the same, and new objects
each contain a draw() method for that particular shape.
XVI. Case Study: Inheriting Interface and Implementation
A. Fig. 7.10, pp. 383-387
XVII. Case Study: Creating and Using Interfaces
public class Point implements Shape {...
A. Shape, which was an abstract superclass, is now an interface
B. An interface defintion begins with the keyword interface and
contains a set of public abstact methods, and may also contain
public final static data.
1. Fig. 7.11, p. 389
C. To use an interface, a class must specify that it 'implements' the
interface and that class must define every method in the interface
with the number of arguments and the return type specified in
the interface definition.
D. If the class leaves one method in the interface undefined, the
class becomes an 'abstract' class and must be declared 'abstract'
in the first line of its class definition.
E. An interface is typically used in place of an abstract class when
there is no default implementation to inherit(i.e., no instance
variables and no default method implementations).
F. Like a 'public abstract' class, an 'interface' is usually defined
in a file by itself with the same name as the interface and the
.java extension.
1. Fig. 7.11, p. 389-394
XVIII. Type-Wrapper Classes for Primitive Types
A. Each of the 8 primitive types has a 'type-wrapper' class:
type type wrapper class name
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
B. Each type-wrapper class enables the primitive type to be treated
as an object of class Object; therefore, values of the primitive
data types can be processed polymorphically if they are maintained
as objects of the type-wrapper classes.
C. Each of the numeric type-wrapper classes(Byte, Short, Integer, Long,
Float, and Double) inherits from class Number, and each is 'final',
so their methods are 'final' and may not be overridden.
D. Since many of the methods that process the primitive data types are
defined as 'static' methods of the type-wrapper clases, the method
for manipulating a primitive value in a program may already be
defined.
This page was last updated on 02.05.1999