Chapter 8 - Department of Accounting and Information Systems

advertisement
Chapter 8
Additional Inheritance Concepts and
Techniques
Chapter 8 - Additional Inheritance Concepts and Techniques
1
Chapter 8 Topics
• Require a subclass to override a superclass
method by including an abstract method in the
superclass
• Creating a Java interface and using an interface
to require a class to implement methods
• Creating and using custom exceptions that
provide detailed information about an error by
extending the Exception class
• How all Java classes implicitly extend the Object
class
Chapter 8 - Additional Inheritance Concepts and Techniques
2
Introducing the Initial Lease Class
• Bradshaw Marina Case Study
– Includes three generalization/specialization
hierarchies that require inheritance
• Boat (Sail, Power), Slip (Covered), Lease (Annual, Daily)
– Lease hierarchy
• Introduces additional inheritance concepts
– Introduces problem domain classes that contain
attributes that hold references to other objects
Chapter 8 - Additional Inheritance Concepts and Techniques
3
Chapter 8 - Additional Inheritance Concepts and Techniques
4
Adding an Abstract Method to Lease
• Abstract Method (Figure 8-5)
– Method without any statements
– Requires that a subclass include or implement
the method
– If a class has an abstract method:
• The class must also be abstract
– Keyword
• abstract
Chapter 8 - Additional Inheritance Concepts and Techniques
5
Implementing the AnnualLease
Subclass
• AnnualLease Class (Figure 8-6)
–
–
–
–
Extends Lease class
Adds attributes
Constructors and methods more complex
Imports java.util package for Date and
Calendar classes
• Both subclass and superclass must do this
Chapter 8 - Additional Inheritance Concepts and Techniques
6
Implementing the DailyLease
Subclass
• DailyLease Class (Figure 8-7)
–
–
–
–
Extends Lease class
Adds attribute
Constructors and methods more complex
Imports java.util package for Date and
Calendar classes
• Both subclass and superclass must do this
Chapter 8 - Additional Inheritance Concepts and Techniques
7
Testing the AnnualLease and
DailyLease Classes
• TesterTwo Class (Figure 8-8)
– Tests AnnualLease and DailyLease classes
• Instantiates objects of both classes
• Instantiates date objects
• Retrieves information about each lease object
Chapter 8 - Additional Inheritance Concepts and Techniques
8
Understanding and Using Java
Interfaces
• Two methods to require that class override
a method:
– Add an abstract method to the superclass that
the class derives from
– Define an interface that the class implements
• Interface
– Java component that defines abstract methods and
constants that must be included by implementing classes
Chapter 8 - Additional Inheritance Concepts and Techniques
9
Understanding and Using Java
Interfaces
• Concept
– Knowing how to use something means
knowing how to interface with it
– The methods an instance can respond to can
be defined as the interface to the instance
– To assure instance has a defined set of
methods:
• Define an interface and have class implement it
Chapter 8 - Additional Inheritance Concepts and Techniques
10
Understanding and Using Java
Interfaces
• Component-based development
– System components interact using welldefined interface built from a variety of
technologies
• If interface is known  can be used in system
• Do not need to know technology used or internal
structure of component
Chapter 8 - Additional Inheritance Concepts and Techniques
11
Understanding and Using Java
Interfaces
• Multiple inheritance
– Ability to “inherit” from more than one class
– Java allows only single inheritance
• Using extends keyword
– Interfaces allow Java an alternative
mechanism to emulate the concept of
multiple inheritance
Chapter 8 - Additional Inheritance Concepts and Techniques
12
Understanding and Using Java
Interfaces
• Creating a Java Interface
– Created much the same way as a class
• Header
– Use interface keyword:
» public interface LeaseInterface
• Abstract methods
– Implement using implements keyword:
• public class AnnualLease extends Lease
implements LeaseInterface
Chapter 8 - Additional Inheritance Concepts and Techniques
13
Chapter 8 - Additional Inheritance Concepts and Techniques
14
Understanding and Using Java
Interfaces
• Implementing More Than One Interface
– Java classes can implement more than one
interface
• Use comma separated list:
– Interfaces can include static final variables
(constants) – Figure 8-11
• All classes that implement the interface have the
constants available
– DailyLease in Figure 8-12
Chapter 8 - Additional Inheritance Concepts and Techniques
15
Understanding and Using Java
Interfaces
• Testing the Complete Interface Example
– TesterThree class (Figure 8-13)
• Tests:
– AnnualLease with one interface
– DailyLease with two interfaces
Chapter 8 - Additional Inheritance Concepts and Techniques
16
Using Custom Exceptions
• Custom exception
– Exception written specifically for a particular
application
– Extends a built-in class
• Any class that is not declared final can be
extended
Chapter 8 - Additional Inheritance Concepts and Techniques
17
Using Custom Exceptions
• Defining the LeasePaymentException (Fig. 8-15)
– Extend custom exception from Exception class
– Add appropriate components:
• Attributes
• Constructors
• Methods
– Sequence diagram
• Shows interaction between the LeasePaymentException
class and the AnnualLease class
Chapter 8 - Additional Inheritance Concepts and Techniques
18
Chapter 8 - Additional Inheritance Concepts and Techniques
19
Using Custom Exceptions
• Throwing a Custom Exception
– AnnualLease class (Figure 8-17)
• Uses LeasePaymentException class
– Use throws keyword in method declaration
– Use throw keyword in method body
Chapter 8 - Additional Inheritance Concepts and Techniques
20
Using Custom Exceptions
• Testing the LeasePaymentException
– TesterFour class
• Tests:
– Revised AnnualLease class
– LeasePaymentException class
– Sequence diagram
• Shows interactions for TesterFour class
Chapter 8 - Additional Inheritance Concepts and Techniques
21
Chapter 8 - Additional Inheritance Concepts and Techniques
22
Chapter 8 - Additional Inheritance Concepts and Techniques
23
Chapter 8 - Additional Inheritance Concepts and Techniques
24
Using Custom Exceptions
• Handling Payments as a Batch
– Batch processing
• Takes a collection of transactions and processes
them one after the other
– If successful:
» Success message is displayed
– If unsuccessful:
» Exception message is displayed (but processing
continues)
• Useful in testing validation and exceptions
Chapter 8 - Additional Inheritance Concepts and Techniques
25
Chapter 8 - Additional Inheritance Concepts and Techniques
26
Chapter 8 - Additional Inheritance Concepts and Techniques
27
Chapter 8 - Additional Inheritance Concepts and Techniques
28
Understanding the Object Class and
Inheritance
• All Java classes extend from Object class
– Object class
• Defines basic functionality that any other class of
objects need in Java
– All built-in class hierarchies have Object as common
ancestor
– All custom problem domain classes extend Object as
well (implicitly)
Chapter 8 - Additional Inheritance Concepts and Techniques
29
Chapter 8 - Additional Inheritance Concepts and Techniques
30
Download