Inheritance and Exception Handling - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Inheritance and Exception Handling

Description:

Learn about checked and unchecked exceptions. Learn how to handle exceptions within a program ... Unchecked Exceptions. Unchecked Exception: exception that ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:1.0/5.0
Slides: 40
Provided by: manasi88
Category:

less

Transcript and Presenter's Notes

Title: Inheritance and Exception Handling


1
Chapter 2
  • Inheritance and Exception Handling

2
Chapter Objectives
  • Learn about inheritance
  • Learn about sub- and superclasses
  • Explore how to override the methods of a
    superclass
  • Examine how constructors of super- and subclasses
    work
  • Examine abstract classes
  • Learn about composition

3
Chapter Objectives
  • 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

4
Inheritance
  • 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

5
Inheritance
6
Inheritance
  • 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

7
Inheritance
  • 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)

8
Defining Constructor of Subclass
  • Call to constructor of superclass
  • must be first statement
  • specified by super parameter list

9
The class Object
  • Directly or indirectly superclass of every class
    in Java
  • Public members of class Object can be
    overridden/invoked by object of any class type

10
The class Object
11
Objects of Superclasses and Subclasses
  • Cannot automatically make reference variable of
    subclass 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 class type
  • ClassCastException thrown if class cast is not
    allowed

12
The Operator instanceof
  • Reference of superclass type can point to objects
    of its subclass
  • Can determine if a reference variable points to
    an object using operator instanceof

13
Abstract Methods and Classes
  • 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

14
Abstract Class
  • 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 abstract methods of superclass

15
Composition
  • Another way to relate two classes
  • One or more members of a class are objects of
    another class type
  • has-a relation between classes

16
Exception
  • 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

17
Java Exception Hierarchy
18
The class Throwable
19
The class Exception and its Subclasses from
java.lang
20
The class Exception and its Subclasses from
java.util
21
The class Exception and its Subclasses from
java.io
22
Javas Exception Class
  • 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 separate
    classes and contained in various packages

23
The class Exception and its Constructors
24
Java Exception Classes
25
Exceptions Thrown by Methods
26
Exceptions Thrown by Methods
27
Checked Exceptions
  • Checked Exception any exception that can be
    analyzed by the compiler
  • Example
  • IOExceptions

28
Unchecked Exceptions
  • Unchecked Exception exception that cannot be
    analyzed when the program compiles (must be
    checked for by programmer)
  • Examples
  • Division by zero
  • Array index out of hounds
  • Syntax
  • throws ExceptionType1, ExceptionType2,
  • ExceptionType1, ExceptionType2, etc are names of
    exception classes

29
Handling Exceptions within a Program
  • 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

30
Handling Exceptions within a Program
  • 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 of exceptions
  • 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

31
Order of Catch Blocks
  • If exception in try block caught by first catch
    block, reaming catch blocks ignored
  • Must be careful about order catch blocks are
    listed

32
Rethrowing and Throwing an Exception
  • 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

33
Exception Handling Techniques
  • 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

34
Creating Your Own Exception Classes
  • Exception class you define extends class
    Exception or one of its subclasses
  • Syntax to throw your own exception object
  • throw new ExceptionClassName(messageString)

35
Programming Example Grade Report
  • Main algorithm
  • Declare variables
  • Open input file
  • Open output file
  • Get number students registered and tuition rate
  • Load students data
  • Print grade reports

36
Programming Example Grade Report
  • Components student, course
  • Operations on course
  • Set course information
  • Print course information
  • Show credit hours
  • Show course number
  • Show grade

37
Programming Example Grade Report
  • 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

38
Chapter Summary
  • Inheritance
  • Single and multiple
  • Rules and Uses
  • Superclasses/subclasses (objects)
  • Overriding/overloading methods
  • The class Object
  • Constructors
  • Rules
  • Abstract methods and classes

39
Chapter Summary
  • Composition
  • Exception
  • Checked and Unchecked Exceptions
  • Creating your own exception classes
  • Exception handling techniques
  • Handling exceptions within a program
Write a Comment
User Comments (0)
About PowerShow.com