Tải bản đầy đủ (.ppt) (37 trang)

tài liệu tự học java cho người mới bắt đầu

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (586.5 KB, 37 trang )

Session 6
Module 8: More on Classes
Module 9: Exceptions
More on class & Exception / Session6 / 2 of 37
Module 7 - Review

Inheritance

Overloading of methods

Using abstract keyword

Using final keywords

Interfaces
More on class & Exception / Session6 / 3 of 37
Module 8 - Objectives

Class variables

Nested class
More on class & Exception / Session6 / 4 of 37
Class variables

Are declared using the static keyword

Once the value is modified, all instances of the class are
updated to share the same value
More on class & Exception / Session6 / 5 of 37
Access Class variables


Can also be manipulated without creating an
instance of the class

Are referenced by the class name itself
More on class & Exception / Session6 / 6 of 37
Static methods

Also known as class methods, do not have
reference to any instance variable in a class, used
to access class variables and methods.

The keyword this cannot be used inside a static
method.
More on class & Exception / Session6 / 7 of 37
Accessing class method

Can be accessed by
using a class name

or

an instance name (not
recommend)
More on class & Exception / Session6 / 8 of 37
Static initializer

Is a block of code embraced
in curly braces

It is preceded by the static

keyword

It initializes the static
variables in class

The static initialization block
can reference only class
variables that have been
declared before it
More on class & Exception / Session6 / 9 of 37
Nested class

A class defined within another class

It can have access to members of the outer class
(enclosing class) even if the members are declared
private

It can be used for:

Allow logical grouping of classes

Increases encapsulation

More maintainable code
More on class & Exception / Session6 / 10 of 37
Different type of nested class

Member classes or non-static nested classes


Local classes

Anonymous classes

Static nested classes
More on class & Exception / Session6 / 11 of 37
Member classes or
non-static nested classes

It can access all fields and methods of the
outer class, but the reverse is not true.

An outer class cannot access a member of
an inner class even if it is declared as public
because members of an inner class are
declared within the scope of inner class
More on class & Exception / Session6 / 12 of 37
Local classes
A local class is declared within a method,
constructor or an initializer.
In other words, a local class is declared within a
block of code and is visible only within that
particular block.
More on class & Exception / Session6 / 13 of 37
Anonymous classes

An anonymous class does not have a name. It is a type of local
class.
More on class & Exception / Session6 / 14 of 37
Static nested classes


A static nested class is
associated with its outer class.

It cannot refer directly to
instance variables or methods
defined in its enclosing class,
but it can access class
methods or variables directly.

An object needs to be
instantiated to access the
instance variables and
methods of the enclosing
class.
More on class & Exception / Session6 / 15 of 37
Module 9 - Objectives

Introduction to Exceptions

Exception handling in Java

User defined exceptions

Assertions
More on class & Exception / Session6 / 16 of 37
Introduction to Exceptions

Explain the concept of Exceptions


Identify the different types of Exceptions

Knowledge Check
More on class & Exception / Session6 / 17 of 37
Explain the concept of Exceptions

An exception are errors that
arises at runtime.

Exceptions object describles
detail of error.
More on class & Exception / Session6 / 18 of 37
Causes for Exceptions

Program errors

Arise due to errors present in APIs, such as NullPointerException

Program using these APIs cannot do anything about these errors

Client code errors

Arise when client code attempts operations, such as reading content
from a file without opening it.

Exception will provide information about the cause of the error

Can be handled by client code

Errors beyond the control of a program


Raised by runtime environment, such as memory error or network
connection failure error.

These exceptions that are not expected to be caught by the client
code
More on class & Exception / Session6 / 19 of 37
Classification of Exceptions

Checked Exceptions

Unchecked
Exceptions
More on class & Exception / Session6 / 20 of 37
Checked Exceptions

Generated in situations that occur during the normal
execution of a program. For example:

Requesting for missing file, invalid user input, and network
failures.

These exceptions should be handled to avoid
compile-time errors. It is checked during the
compilation.
More on class & Exception / Session6 / 21 of 37
Types of Checked Exceptions
All Checked exceptions are derived from the Exception class.
More on class & Exception / Session6 / 22 of 37
Unchecked Exceptions


Generated in situations that are considered non-recoverable for
a program (generated during the execution of the program). For
example:

Attempting to access an element beyond the maximum length of
an array.

An application is not required to check for these kinds of
exceptions.
Unchecked Exceptions
RuntimeException class Error class
(ex: logical bugs)
(ex: Out Of Memory Error)
More on class & Exception / Session6 / 23 of 37
Types of Unchecked Exceptions
All Unchecked exceptions are directly or indirectly derived from the
RuntimeException class
More on class & Exception / Session6 / 24 of 37
Exception handling in Java

State the use of try-catch block

Describe the use of finally block

State the flow of execution in an exception
handling block

Describe the use of throw and throws
keyword


Describe the use of multiple catch block
More on class & Exception / Session6 / 25 of 37
The use of try-catch block
try {
Statement_1;
Statement_2;

}
catch(ExceptionType ObjectName){
Statement_1;
}
Syntax

×