Download Chapter 10 Classes Continued

Document related concepts
no text concepts found
Transcript
Chapter 10
Classes Continued
Fundamentals of Java
Objectives



2
Know when it is appropriate to include class
(static) variables and methods in a class.
Understand the role of Java interfaces in a
software system and define an interface for
a set of implementing classes.
Understand the use of inheritance by
extending a class.
Fundamentals of Java
Objectives (cont.)


3
Understand the use of polymorphism and
know how to override methods in a
superclass.
Place the common features (variables and
methods) of a set of classes in an abstract
class.
Fundamentals of Java
Objectives (cont.)


4
Understand the implications of reference
types for equality, copying, and mixed-mode
operations.
Know how to define and use methods that
have preconditions, postconditions, and
throw exceptions.
Fundamentals of Java
Vocabulary







5
Abstract class
Abstract method
Aggregation
Class (static) method
Class (static) variable
Concrete class
Dependency
Fundamentals of Java
Vocabulary (cont.)






6
Final method
Inheritance
Interface
Overriding
Postcondition
Precondition
Fundamentals of Java
Class (static) Variables and
Methods

static variables and methods belong to a
class.
–

Class variable: Storage allocated at
program startup
–

7
Not an instance of the class
Independent of number of instances created
Class method: Activated when message
sent to the class rather than to an object
Fundamentals of Java
Class (static) Variables and
Methods (cont.)

Class variables and methods are declared
with the keyword static.

Example:
–
private static int studentCount = 0;


8
Shared by all instances of the class with this
declaration
Use a static variable in any situation in which
all instances share a common data value.
Fundamentals of Java
Class (static) Variables and
Methods (cont.)


Use static methods to provide public access
to static variables.
Class constants: Combine keyword final
with keyword static
–
Example:

9
public static final int MIN_SCORE = 0;
Fundamentals of Java
Class (static) Variables and
Methods (cont.)
10
Static variables, methods, and constants example
Fundamentals of Java
Class (static) Variables and
Methods (cont.)
11
Static variables, methods, and constants example (cont.)
Fundamentals of Java
Class (static) Variables and
Methods (cont.)
12

Using class variables example:

Using class constants example:
Fundamentals of Java
Class (static) Variables and
Methods (cont.)

Two rules for using static variables:
–
Class methods can reference only static
variables.
 Never
–

Instance methods can reference static and
instance variables.
The main method for an executable java
class is static.
–
13
instance variables
JVM sends main message to start a program.
Fundamentals of Java
Turtle Graphics

Open-source Java package for drawing
–
–
Used in this text to illustrate features of objectoriented programming
Pen used for drawing on a window
 StandardPen
14
is a specific type of Pen.
Fundamentals of Java
Turtle Graphics (cont.)
15
Table 10-1: Pen messages
Fundamentals of Java
Turtle Graphics (cont.)
16
Example 10.1: Drawing a square using Turtle Graphics
Fundamentals of Java
Java Interfaces—The Client
Perspective

Interface: A list of a class’s public methods
–
–

In Turtle Graphics, Pen is an interface.
–
17
Provides information to use a class without
revealing its implementation
When related classes have same interface, they
can be used interchangeably in a program.
StandardPen, WigglePen, and RainbowPen
are examples of classes that conform to the
Pen interface.
Fundamentals of Java
Java Interfaces—The Client
Perspective (cont.)

18
A Java interface specifies the method
signatures for an interface.
Fundamentals of Java
Java Interfaces—The Client
Perspective (cont.)

An interface is not a class.
–

19
But can be used as a data type
Having multiple classes conform to the same
interface allows for polymorphic behavior:
Fundamentals of Java
Java Interfaces—The Client
Perspective (cont.)


A class that conforms to an interface is said
to implement the interface.
When declaring a variable or parameter, use
the interface type when possible.
–
–
20
Methods using interface types are more
general.
They are easier to maintain.
Fundamentals of Java
Java Interfaces—The
Implementation Perspective

21
A class
implements an
interface using
the implements
keyword.
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)


22
A class that implements an interface must
implement every method in the interface.
A variable declared with the interface type
can reference objects of any class that
implements the interface.
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)
23
The Circle class
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)
The Circle class (cont.)
24
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)
Example 10.3: Try out some shapes
25
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)
26
Example 10.3: Try out some shapes (cont.)
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)
Figure 10.3: Output from the TestShapes program
27
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)

Important interface concepts:
–
–
–
28
Interface contains only methods, never
variables.
Interface methods are usually public.
If more than one class implements an interface,
its methods are polymorphic.
Fundamentals of Java
Java Interfaces – The
Implementation Perspective (cont.)

Important interface concepts (cont.):
–
–
–
29
A class can implement methods in addition to
those listed in the interface.
A class can implement more than one interface.
Interfaces can be in an inheritance hierarchy.
Fundamentals of Java
Code Reuse Through Inheritance

All classes are part of a large class hierarchy.
–
Object class is the root.
–
Each class inherits variables and methods of
the classes above it in the hierarchy.
 New
classes can add new variables, add new
methods, or alter existing inherited methods.
–
–
30
Class immediately above a class is a
superclass.
Any classes that inherit are subclasses.
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)


A class can only have one superclass, but
may have many subclasses.
The descendents of a class consist of its
subclasses, their subclasses, etc.
Figure 10-4: Part of a class hierarchy
31
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)

32
A class can inherit the characteristics of another
class using the extends keyword.
The Wheel class extends the Circle class.
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)
33
The Wheel class extends the Circle class (cont.).
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)
Example 10.4: Draw a wheel and a circle.
34
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)
Figure 10-5: A circle and a wheel with the
same radius but different positions
35
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)


Wheel implements Shape because it
extends Circle, which implements Shape.
xPos, yPos, and radius inherited by
Wheel from Shape
–
Must modify these instance variables in Shape
to be protected rather than private
 Protected
access modifier means variables
are accessible only in current class and its
descendents.
36
Fundamentals of Java
Code Reuse Through Inheritance
(cont.)
37

Methods may also be protected.

A constructor may call superclass constructor
using super();.

To call one of superclass methods:

Overriding: A subclass can modify a
superclass method by re-implementing it.
Fundamentals of Java
Inheritance and Abstract Classes

Abstract class: Provides functionality
(methods), but can never be instantiated
–
–
–
38
Can only be extended
Declared with keyword abstract
May contain standard methods and abstract
methods
Fundamentals of Java
Inheritance and Abstract Classes
(cont.)

Abstract method: A method with no body
–
Defined using keyword abstract
–
A class with an abstract method will also be
abstract.
A subclass of an abstract class must implement
every abstract method in that class.
–

39
Final methods: Methods that cannot be
overridden in a subclass
Fundamentals of Java
Inheritance and Abstract Classes
(cont.)

40
Partial abstract class/method example:
Fundamentals of Java
Interfaces, Inheritance, and
Relationships among Classes



41
A Java interface has a name and consists of
a list of method headers.
One or more classes can implement the
same interface.
If a variable is declared as an interface type,
it can be associated with an object from any
class that implements the interface.
Fundamentals of Java
Interfaces, Inheritance, and
Relationships among Classes (cont.)


If a class implements an interface, then all of
its subclasses do so implicitly.
A subclass inherits all of the characteristics of
its superclass.
–

42
Subclass can add new variables and methods or
modify inherited methods.
Characteristics common to several classes
can be collected in a common abstract
superclass that is never instantiated.
Fundamentals of Java
Interfaces, Inheritance, and
Relationships among Classes (cont.)



43
An abstract class can contain headers for
abstract methods that are implemented in the
subclasses.
A class’s constructors and methods can use
constructors and methods in the superclass.
Inheritance reduces repetition and promotes
the reuse of code.
Fundamentals of Java
Interfaces, Inheritance, and
Relationships among Classes (cont.)


Interfaces and inheritance promote the use
of polymorphism.
When a message is sent to an object, Java
looks for a matching method.
–
44
Search starts in the object’s class and, if
necessary, continues up the class hierarchy
Fundamentals of Java
Interfaces, Inheritance, and
Relationships among Classes (cont.)

Four ways in which methods in a subclass
can be related to methods in a superclass:
–
–
–
–

45
Implementation of an abstract method
Extension
Overriding
Finality
It is possible to work only with abstract
classes, forgoing interfaces.
Fundamentals of Java
Interfaces, Inheritance, and
Relationships among Classes (cont.)
46
Figure 10-7: Three types of relationships among classes
Fundamentals of Java
Acceptable Classes for
Parameters and Return Values

If an object of class BBB is expected, it is
always acceptable to substitute an object of a
subclass.
–
But never of a superclass
 A subclass
of BBB inherits all of BBB’s methods.
 No
guarantees about the methods in the
superclass
47
Fundamentals of Java
Error Handling with Classes


Before implementing error handling code,
must determine error conditions for a class
Preconditions: Describe what must be true
before a particular method is called
–

Postconditions: Describe what must be true
after a particular method has been executed
–
48
Values for parameters/instance variables
Return values and altered instance variables
Fundamentals of Java
Error Handling with Classes
(cont.)

49
Example:
Fundamentals of Java
Exceptions


50
JVM can throw exceptions when illegal
operations are attempted.
Commonly used exception classes:
Fundamentals of Java
Exceptions (cont.)

Can throw exceptions in methods:
–
51
To enforce pre-conditions or any other condition

Syntax:

Example:
Fundamentals of Java
Exceptions (cont.)

52
Exceptions to enforce pre-conditions:
Fundamentals of Java
Exceptions (cont.)

Clients who call such methods need to catch
the exceptions so the program does not halt.
–
53
Embed method call in a try-catch statement
Fundamentals of Java
Exceptions (cont.)

A method may throw more than one type of
exception.
–

54
Client can handle each type explicitly.
Example:
Fundamentals of Java
Reference Types, Equality,
and Object Identity


Aliasing: Two reference variables point to
same object
Comparing objects for equality:
–
–
55
Comparing two reference variables using ==
indicates whether the variables point to the
same object.
To compare values of two distinct objects for
equality, use the equals method.
Fundamentals of Java
Reference Types, Equality,
and Object Identity (cont.)

equals method: Defined in Object class
–
–

56
Uses == operator by default
A class must override equals to allow for
comparison of object’s contents.
Example:
Fundamentals of Java
Reference Types, Equality,
and Object Identity (cont.)

Copying objects:
–
Aliasing is not copying.
Use a copy constructor:
–
Implement Clonable:
–
57
Fundamentals of Java
Summary



Class (static) variables provide storage for
data that all instances of a class can access
but do not have to own separately.
Class (static) methods are written primarily
for class variables.
An interface specifies a set of methods that
implementing classes must include.
–
58
Gives clients enough information to use a class
Fundamentals of Java
Summary (cont.)



59
Polymorphism and inheritance reduce the
amount of code that must be written by
servers and learned by clients.
Classes that extend other classes inherit
their data and methods.
Methods in different classes that have the
same name are polymorphic.
Fundamentals of Java
Summary (cont.)

Abstract classes are not instantiated.
–
–

60
Help organize related subclasses
Contain their common data and methods
Error handling can be distributed among
methods and classes by using preconditions,
postconditions, and exceptions.
Fundamentals of Java
Summary (cont.)

Because of the possibility of aliasing, the
programmer should provide:
–
–
61
equals method for comparing for equality
clone method for creating a copy of an object
Fundamentals of Java