dschap02

39
IAS1223 DATA STRUCTURES AND ALGORITHMS Chapter 2 Inherit ance and ex ception handling 1

Upload: muhammad-hashim

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 1/39

IAS1223 DATA STRUCTURES AND ALGORITHMS

Chapter 2

Inheritance and exception

handling

1

Page 2: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 2/39

Chapter Objectives

Data Structures Using Java

2

Learn about inheritance

Learn about sub- and superclasses

Explore how to override the methods of a superclass

Examine how constructors of super- and subclasseswork

Examine abstract classes

Learn about composition

Page 3: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 3/39

Chapter Objectives

Data Structures Using Java

3

Learn about exceptions

Become aware of exception classes and their

hierarchy

Learn about checked and unchecked exceptions

Learn how to handle exceptions within a program

Examine try/catch blocks

Discover how to throw and rethrow an exception

Page 4: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 4/39

Inheritance

Data Structures Using Java

4

´is-aµ relationship

Single inheritance

subclass derived from one existing class (superclass)

Multiple inheritance

Subclass derived from more than one superclass

Not supported by Java

In Java, a class can only extend definition of one class

Page 5: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 5/39

Inheritance

Data Structures Using Java 5

Page 6: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 6/39

Inheritance

Data Structures Using Java

6

Private members of superclass

private to superclass; cannot be accessed directly by

subclass

Subclass can override method of superclass;

redefinition applies only to object of subclass

Page 7: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 7/39

Inheritance

Data Structures Using Java

7

To write method definition of subclass to specify call

to public method of superclass

If subclass overrides public method of superclass, to specify

call to public method of superclass:super.MethodName(parameter list)

If subclass does not override public method of superclass, to

specify call to public method of superclass:

MethodName(parameter list)

Page 8: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 8/39

Defining Constructor of Subclass

Data Structures Using Java

8

Call to constructor of superclass

must be first statement

specified by: super parameter list

Page 9: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 9/39

The class Object

Data Structures Using Java

9

Directly or indirectly superclass of every class in

Java

Public members of class Object can be

overridden/invoked by object of any class type

Page 10: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 10/39

The class Object

Data Structures Using Java

10

Page 11: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 11/39

Objects of Superclasses and Subclasses

Data Structures Using Java

11

Cannot automatically make reference variable ofsubclass type point to object of superclass

Dynamic binding: method executed determined at

execution time, not compile time Operator instanceof : determines whether reference

variable that points to object is of particular classtype

ClassCastException thrown if class cast is not allowed

Page 12: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 12/39

The Operator instanceof

Data Structures Using Java

12

Reference of superclass type can point to objects of

its subclass

Can determine if a reference variable points to an

object using operator instanceof

Page 13: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 13/39

Abstract Methods and Classes

Data Structures Using Java

13

Abstract method: method that has only the heading

with no body

must be declared abstract

Abstract class: class that is declared with the reserved

word abstract in its heading

Page 14: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 14/39

Abstract Class

Data Structures Using Java

14

Can contain instance variables, constructors,finalizer, abstract and nonabstract methods

Cannot instantiate object of abstract class type; can

only declare reference variable Can instantiate object of subclass of abstract class,

but only if subclass gives definitions of all abstractmethods of superclass

Page 15: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 15/39

Composition

Data Structures Using Java

15

Another way to relate two classes

One or more members of a class are objects of

another class type

´has-aµ relation between classes

Page 16: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 16/39

Exception

Data Structures Using Java

16

Definition: an occurrence of an undesirable

situation that can be detected during program

execution

Examples

division by zero

trying to open an input file that does not exist is an

exception an array index that goes out of bounds

Page 17: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 17/39

Java Exception Hierarchy

Data Structures Using Java

17

Page 18: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 18/39

The class Throwable

Data Structures Using Java

18

Page 19: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 19/39

The class Exception and its Subclasses

from java.lang

Data Structures Using Java 19

Page 20: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 20/39

The class Exception and its Subclasses

from java.util

Data Structures Using Java

20

Page 21: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 21/39

The class Exception and its Subclasses

from java.io

Data Structures Using Java 21

Page 22: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 22/39

Java·s Exception Class

Data Structures Using Java

22

Class Exception Subclass of class Throwable

superclass of classes designed to handle exceptions

Various types of exceptions I/O exceptions

Number format exceptions

File not found exceptions

Array index out of bounds exception

Various exceptions categorized into separateclasses and contained in various packages

Page 23: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 23/39

The class Exception and its Constructors

Data Structures Using Java

23

Page 24: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 24/39

Java Exception Classes24

Data Structures Using Java

Page 25: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 25/39

Exceptions Thrown by Methods25

Data Structures Using Java

Page 26: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 26/39

Exceptions Thrown by Methods

Data Structures Using Java

26

Page 27: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 27/39

Checked Exceptions

Data Structures Using Java

27

Checked Exception: any exception that can be

analyzed by the compiler

Example

IOExceptions

Page 28: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 28/39

Unchecked Exceptions

Data Structures Using Java

28

Unchecked Exception: exception that cannot beanalyzed when the program compiles (must bechecked for by programmer)

Examples Division by zero

Array index out of hounds

Syntax

throws ExceptionType1, Exception

Type2, «

*ExceptionType1, ExceptionType2, etc are names of exception classes

Page 29: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 29/39

Handling Exceptions within a Program

Data Structures Using Java

29

try/catch/finally block used to handle exceptions

within a program

try block:

Includes statements that may generate an exception

Includes statements that should not be executed if an

exception occurs

followed by zero or more catch blocks

May or may not be followed by finally block

Page 30: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 30/39

Handling Exceptions within a Program

Data Structures Using Java

30

Catch block: heading specifies type of exception it can catch

contains exception handler; completely handles exception

can catch all exceptions of a specific type or all types ofexceptions

may or may not be followed by a finally block

Finally block

Code contained in this block always executes try block with no catch block has finally block

Page 31: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 31/39

Order of Catch Blocks

Data Structures Using Java

31

If exception in try block caught by first catch block,

reaming catch blocks ignored

Must be careful about order catch blocks are listed

Page 32: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 32/39

Rethrowing and Throwing an

Exception

Data Structures Using Java

32

Useful when

Catch block catches exception but is unable to handle it

Catch block decides exception should be handled by

calling environment

Allows programmer to provide exception handling

code in one place

Page 33: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 33/39

Exception Handling Techniques

Data Structures Using Java

33

Terminate program

Output appropriate error message upon termination

Fix error and continue

Repeatedly get user input/output appropriate error

message until valid value is entered

Log error and continue

Write error messages to file and continue with program

execution

Page 34: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 34/39

Creating Your Own Exception

Classes

Data Structures Using Java

34

Exception class you define extends class

Exception or one of its subclasses

Syntax to throw your own exception object

throw new ExceptionClassName(messageString);

Page 35: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 35/39

Programming Example: Grade

Report

Data Structures Using Java

35

Main algorithm

Declare variables

Open input file

Open output file

Get number students registered and tuition rate

Load students· data

Print grade reports

Page 36: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 36/39

Programming Example: Grade

Report

Data Structures Using Java

36

Components: student, course

Operations on course

Set course information

Print course information

Show credit hours

Show course number

Show grade

Page 37: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 37/39

Programming Example: Grade

Report

Data Structures Using Java

37

Operations on student

Set student information

Print student information

Calculate number of credit hours taken

Calculate GPA

Calculate billing amount

Sort the courses according to the course number

Page 38: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 38/39

Chapter Summary

Data Structures Using Java

38

Inheritance Single and multiple

Rules and Uses

Superclasses/subclasses (objects) Overriding/overloading methods

The class Object

Constructors

Rules Abstract methods and classes

Page 39: DSchap02

8/3/2019 DSchap02

http://slidepdf.com/reader/full/dschap02 39/39

Chapter Summary

Data Structures Using Java

39

Composition

Exception

Checked and Unchecked Exceptions

Creating your own exception classes

Exception handling techniques

Handling exceptions within a program