Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.

Similar presentations


Presentation on theme: "Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques."— Presentation transcript:

1 Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques

2 Chapter 8 - Additional Inheritance Concepts and Techniques2 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

3 Chapter 8 - Additional Inheritance Concepts and Techniques3 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

4 Chapter 8 - Additional Inheritance Concepts and Techniques4

5 5 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

6 Chapter 8 - Additional Inheritance Concepts and Techniques6 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

7 Chapter 8 - Additional Inheritance Concepts and Techniques7 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

8 Chapter 8 - Additional Inheritance Concepts and Techniques8 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

9 Chapter 8 - Additional Inheritance Concepts and Techniques9 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

10 Chapter 8 - Additional Inheritance Concepts and Techniques10 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

11 Chapter 8 - Additional Inheritance Concepts and Techniques11 Understanding and Using Java Interfaces Component-based development –System components interact using well- defined 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

12 Chapter 8 - Additional Inheritance Concepts and Techniques12 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

13 Chapter 8 - Additional Inheritance Concepts and Techniques13 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

14 Chapter 8 - Additional Inheritance Concepts and Techniques14

15 Chapter 8 - Additional Inheritance Concepts and Techniques15 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

16 Chapter 8 - Additional Inheritance Concepts and Techniques16 Understanding and Using Java Interfaces Testing the Complete Interface Example –TesterThree class (Figure 8-13) Tests: –AnnualLease with one interface –DailyLease with two interfaces

17 Chapter 8 - Additional Inheritance Concepts and Techniques17 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

18 Chapter 8 - Additional Inheritance Concepts and Techniques18 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

19 Chapter 8 - Additional Inheritance Concepts and Techniques19

20 Chapter 8 - Additional Inheritance Concepts and Techniques20 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

21 Chapter 8 - Additional Inheritance Concepts and Techniques21 Using Custom Exceptions Testing the LeasePaymentException –TesterFour class Tests: –Revised AnnualLease class –LeasePaymentException class –Sequence diagram Shows interactions for TesterFour class

22 Chapter 8 - Additional Inheritance Concepts and Techniques22

23 Chapter 8 - Additional Inheritance Concepts and Techniques23

24 Chapter 8 - Additional Inheritance Concepts and Techniques24

25 Chapter 8 - Additional Inheritance Concepts and Techniques25 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

26 Chapter 8 - Additional Inheritance Concepts and Techniques26

27 Chapter 8 - Additional Inheritance Concepts and Techniques27

28 Chapter 8 - Additional Inheritance Concepts and Techniques28

29 Chapter 8 - Additional Inheritance Concepts and Techniques29 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)

30 Chapter 8 - Additional Inheritance Concepts and Techniques30


Download ppt "Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques."

Similar presentations


Ads by Google