Академический Документы
Профессиональный Документы
Культура Документы
Chapter Objectives
2
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
Chapter Objectives
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
Inheritance
4
Multiple inheritance
Subclass derived from more than one superclass Not supported by Java In Java, a class can only extend definition of one class
Inheritance
Inheritance
6
Subclass can override method of superclass; redefinition applies only to object of subclass
Inheritance
7
Directly or indirectly superclass of every class in Java Public members of class Object can be overridden/invoked by object of any class type
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
Reference of superclass type can point to objects of its subclass Can determine if a reference variable points to an object using operator instanceof
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
Abstract Class
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 abstract methods of superclass
Composition
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
Exception
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
19
21
Class Exception
Subclass of class Throwable superclass of classes designed to handle exceptions
Various exceptions categorized into separate classes and contained in various packages
Data Structures Using Java
Checked Exceptions
27
Checked Exception: any exception that can be analyzed by the compiler Example
IOExceptions
Unchecked Exceptions
28
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
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
If exception in try block caught by first catch block, reaming catch blocks ignored Must be careful about order catch blocks are listed
32
block catches exception but is unable to handle it Catch block decides exception should be handled by calling environment
Terminate program
Output appropriate error message upon termination Repeatedly get user input/output appropriate error message until valid value is entered Write error messages to file and continue with program execution
34
Main algorithm
Declare
variables Open input file Open output file Get number students registered and tuition rate Load students data Print grade reports
course information Print course information Show credit hours Show course number Show grade
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
Chapter Summary
38
Inheritance
Single and multiple Rules and Uses Superclasses/subclasses (objects) Overriding/overloading methods
Chapter Summary
39
Composition Exception Checked and Unchecked Exceptions Creating your own exception classes Exception handling techniques Handling exceptions within a program