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