Download - Chapter 10 Classes Continued
![Page 1: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/1.jpg)
Chapter 10Classes Continued
Fundamentals of Java
![Page 2: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/2.jpg)
Fundamentals of Java 2
Objectives
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.
![Page 3: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/3.jpg)
Fundamentals of Java 3
Objectives (cont.)
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.
![Page 4: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/4.jpg)
Fundamentals of Java 4
Objectives (cont.)
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.
![Page 5: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/5.jpg)
Fundamentals of Java 5
Vocabulary
Abstract class Abstract method Aggregation Class (static) method Class (static) variable Concrete class Dependency
![Page 6: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/6.jpg)
Fundamentals of Java 6
Vocabulary (cont.)
Final method Inheritance Interface Overriding Postcondition Precondition
![Page 7: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/7.jpg)
Fundamentals of Java 7
Class (static) Variables and Methods
static variables and methods belong to a class.– Not an instance of the class
Class variable: Storage allocated at program startup – Independent of number of instances created
Class method: Activated when message sent to the class rather than to an object
![Page 8: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/8.jpg)
Fundamentals of Java 8
Class (static) Variables and Methods (cont.)
Class variables and methods are declared with the keyword static.
Example:– private static int studentCount = 0;
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.
![Page 9: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/9.jpg)
Fundamentals of Java 9
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:
public static final int MIN_SCORE = 0;
![Page 10: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/10.jpg)
Fundamentals of Java 10
Class (static) Variables and Methods (cont.)
Static variables, methods, and constants example
![Page 11: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/11.jpg)
Fundamentals of Java 11
Class (static) Variables and Methods (cont.)
Static variables, methods, and constants example (cont.)
![Page 12: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/12.jpg)
Fundamentals of Java 12
Class (static) Variables and Methods (cont.)
Using class variables example:
Using class constants example:
![Page 13: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/13.jpg)
Fundamentals of Java 13
Class (static) Variables and Methods (cont.)
Two rules for using static variables:– Class methods can reference only static
variables. Never instance variables
– Instance methods can reference static and instance variables.
The main method for an executable java class is static.– JVM sends main message to start a program.
![Page 14: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/14.jpg)
Fundamentals of Java 14
Turtle Graphics
Open-source Java package for drawing– Used in this text to illustrate features of object-
oriented programming– Pen used for drawing on a window
StandardPen is a specific type of Pen.
![Page 15: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/15.jpg)
Fundamentals of Java 15
Turtle Graphics (cont.)
Table 10-1: Pen messages
![Page 16: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/16.jpg)
Fundamentals of Java 16
Turtle Graphics (cont.)
Example 10.1: Drawing a square using Turtle Graphics
![Page 17: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/17.jpg)
Fundamentals of Java 17
Java Interfaces—The Client Perspective
Interface: A list of a class’s public methods– Provides information to use a class without
revealing its implementation– When related classes have same interface, they
can be used interchangeably in a program. In Turtle Graphics, Pen is an interface.
– StandardPen, WigglePen, and RainbowPen are examples of classes that conform to the Pen interface.
![Page 18: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/18.jpg)
Fundamentals of Java 18
Java Interfaces—The Client Perspective (cont.)
A Java interface specifies the method signatures for an interface.
![Page 19: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/19.jpg)
Fundamentals of Java 19
Java Interfaces—The Client Perspective (cont.)
An interface is not a class.– But can be used as a data type
Having multiple classes conform to the same interface allows for polymorphic behavior:
![Page 20: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/20.jpg)
Fundamentals of Java 20
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.– Methods using interface types are more
general.– They are easier to maintain.
![Page 21: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/21.jpg)
Fundamentals of Java 21
Java Interfaces—The Implementation Perspective
A class implements an interface using the implements keyword.
![Page 22: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/22.jpg)
Fundamentals of Java 22
Java Interfaces – The Implementation Perspective (cont.)
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.
![Page 23: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/23.jpg)
Fundamentals of Java 23
Java Interfaces – The Implementation Perspective (cont.)
The Circle class
![Page 24: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/24.jpg)
Fundamentals of Java 24
Java Interfaces – The Implementation Perspective (cont.)
The Circle class (cont.)
![Page 25: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/25.jpg)
Fundamentals of Java 25
Java Interfaces – The Implementation Perspective (cont.)
Example 10.3: Try out some shapes
![Page 26: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/26.jpg)
Fundamentals of Java 26
Java Interfaces – The Implementation Perspective (cont.)
Example 10.3: Try out some shapes (cont.)
![Page 27: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/27.jpg)
Fundamentals of Java 27
Java Interfaces – The Implementation Perspective (cont.)
Figure 10.3: Output from the TestShapes program
![Page 28: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/28.jpg)
Fundamentals of Java 28
Java Interfaces – The Implementation Perspective (cont.)
Important interface concepts:– Interface contains only methods, never
variables.– Interface methods are usually public.– If more than one class implements an interface,
its methods are polymorphic.
![Page 29: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/29.jpg)
Fundamentals of Java 29
Java Interfaces – The Implementation Perspective (cont.)
Important interface concepts (cont.):– 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.
![Page 30: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/30.jpg)
Fundamentals of Java 30
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.– Class immediately above a class is a superclass.– Any classes that inherit are subclasses.
![Page 31: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/31.jpg)
Fundamentals of Java 31
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
![Page 32: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/32.jpg)
Fundamentals of Java 32
Code Reuse Through Inheritance (cont.)
A class can inherit the characteristics of another class using the extends keyword.
The Wheel class extends the Circle class.
![Page 33: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/33.jpg)
Fundamentals of Java 33
Code Reuse Through Inheritance (cont.)
The Wheel class extends the Circle class (cont.).
![Page 34: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/34.jpg)
Fundamentals of Java 34
Code Reuse Through Inheritance (cont.)
Example 10.4: Draw a wheel and a circle.
![Page 35: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/35.jpg)
Fundamentals of Java 35
Code Reuse Through Inheritance (cont.)
Figure 10-5: A circle and a wheel with the same radius but different positions
![Page 36: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/36.jpg)
Fundamentals of Java 36
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 privateProtected access modifier means variables
are accessible only in current class and its descendents.
![Page 37: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/37.jpg)
Fundamentals of Java 37
Code Reuse Through Inheritance (cont.)
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.
![Page 38: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/38.jpg)
Fundamentals of Java 38
Inheritance and Abstract Classes
Abstract class: Provides functionality (methods), but can never be instantiated– Can only be extended– Declared with keyword abstract– May contain standard methods and abstract
methods
![Page 39: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/39.jpg)
Fundamentals of Java 39
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. Final methods: Methods that cannot be
overridden in a subclass
![Page 40: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/40.jpg)
Fundamentals of Java 40
Inheritance and Abstract Classes (cont.)
Partial abstract class/method example:
![Page 41: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/41.jpg)
Fundamentals of Java 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.
Interfaces, Inheritance, and Relationships among Classes
![Page 42: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/42.jpg)
Fundamentals of Java 42
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.– 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.
![Page 43: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/43.jpg)
Fundamentals of Java 43
Interfaces, Inheritance, and Relationships among Classes (cont.)
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.
![Page 44: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/44.jpg)
Fundamentals of Java 44
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.– Search starts in the object’s class and, if
necessary, continues up the class hierarchy
![Page 45: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/45.jpg)
Fundamentals of Java 45
Interfaces, Inheritance, and Relationships among Classes (cont.)
Four ways in which methods in a subclass can be related to methods in a superclass:– Implementation of an abstract method– Extension– Overriding– Finality
It is possible to work only with abstract classes, forgoing interfaces.
![Page 46: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/46.jpg)
Fundamentals of Java 46
Interfaces, Inheritance, and Relationships among Classes (cont.)
Figure 10-7: Three types of relationships among classes
![Page 47: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/47.jpg)
Fundamentals of Java 47
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
![Page 48: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/48.jpg)
Fundamentals of Java 48
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– Values for parameters/instance variables
Postconditions: Describe what must be true after a particular method has been executed– Return values and altered instance variables
![Page 49: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/49.jpg)
Fundamentals of Java 49
Error Handling with Classes (cont.)
Example:
![Page 50: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/50.jpg)
Fundamentals of Java 50
Exceptions
JVM can throw exceptions when illegal operations are attempted.
Commonly used exception classes:
![Page 51: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/51.jpg)
Fundamentals of Java 51
Exceptions (cont.)
Can throw exceptions in methods:– To enforce pre-conditions or any other condition
Syntax:
Example:
![Page 52: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/52.jpg)
Fundamentals of Java 52
Exceptions (cont.)
Exceptions to enforce pre-conditions:
![Page 53: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/53.jpg)
Fundamentals of Java 53
Exceptions (cont.)
Clients who call such methods need to catch the exceptions so the program does not halt.– Embed method call in a try-catch statement
![Page 54: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/54.jpg)
Fundamentals of Java 54
Exceptions (cont.)
A method may throw more than one type of exception.– Client can handle each type explicitly.
Example:
![Page 55: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/55.jpg)
Fundamentals of Java 55
Reference Types, Equality, and Object Identity
Aliasing: Two reference variables point to same object
Comparing objects for equality:– 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.
![Page 56: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/56.jpg)
Fundamentals of Java 56
Reference Types, Equality, and Object Identity (cont.)
equals method: Defined in Object class– Uses == operator by default– A class must override equals to allow for
comparison of object’s contents. Example:
![Page 57: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/57.jpg)
Fundamentals of Java 57
Reference Types, Equality, and Object Identity (cont.)
Copying objects:– Aliasing is not copying.– Use a copy constructor:
– Implement Clonable:
![Page 58: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/58.jpg)
Fundamentals of Java 58
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.– Gives clients enough information to use a class
![Page 59: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/59.jpg)
Fundamentals of Java 59
Summary (cont.)
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.
![Page 60: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/60.jpg)
Fundamentals of Java 60
Summary (cont.)
Abstract classes are not instantiated.– 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.
![Page 61: Chapter 10 Classes Continued](https://reader030.vdocuments.us/reader030/viewer/2022012900/56815ad9550346895dc8a59f/html5/thumbnails/61.jpg)
Fundamentals of Java 61
Summary (cont.)
Because of the possibility of aliasing, the programmer should provide: – equals method for comparing for equality– clone method for creating a copy of an object