Java NewGen Day3
Java NewGen Day3
Packages in Java
Packages in Java
Packages in Java
Steps For Creating an User Defined Package
Access Specifiers
The access modifiers in Java specifies the accessibility or scope of a field, method,
constructor, or class. We can change the access level of fields, constructors, methods, and
class by applying the access modifier on it.
There are four types of Java access modifiers:
private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.
Programming in Java
Access Specifiers
Access Modifier within class within package outside package outside package
by subclass only
private Y N N N
default Y Y N N
protected Y Y Y N
public Y Y Y Y
We can use other modifiers as well like static , abstract or final. But these are
considered as modifiers.
Programming in Java
Exception Handling in Java
Exceptions that arise as a result of environmental issues that are rare enough or hard
enough to recover from are called errors.
The Exception class is the base class that represents checked and unchecked
exceptions. It is already defined in java.lang package.
The Error class is the base class used for the unchecked, serious error conditions from
which your program is not expected to attempt recovery.
The RuntimeException class is the base class that is used for the unchecked exceptions
that might arise as a result of program bugs.
Java Script
Exception Handling in Java :An Example
import java.util.Scanner;
// Example of Exception Handling
public class Example1
{
private int firstno,secondno, result;
public void GetData()
{
try
{
Scanner sc=new Scanner(System.in); System.out.println("Enter Two Numbers");
firstno=sc.nextInt(); secondno=sc.nextInt();
result=firstno/(secondno-3); System.out.println("Total = "+result);
}
catch(Exception x)
{
System.out.println("Exception Occurred During Execution");
}
finally { System.out.println("End of Program"); }
}
public static void main(String[] a)
{
Example1 e1=new Example1(); e1.GetData();
}
}
Programming in Java
Exception Handling in Java
The try-catch block:
The try block governs the statements that are enclosed within it and defines the
scope of the exception-handlers associated with it.
A try block must have at least one catch block that follows it immediately.
The catch statement takes the object of the exception class that refers to the
exception caught, as a parameter.
Once the exception is caught, the statements within the catch block are executed.
The scope of the catch block is restricted to the statements in the preceding try
block only.
If an exception is not handled in the current try-catch block, it is thrown to the
caller of the method.
If the exception gets back to the main method and is not handled there, the
program is terminated abnormally
Java Script
Exception Handling in Java
These are special Exception classes and they are all derived from Exception class
Programming in Java
Exception Handling in Java
Trainer Will Give Necessary Explanation and Demo For the Above Topic.
Programming in Java
Assignment:
1. Java program to Display Fibonacci Series upto a certain number- the no. has to be
entered by the user. [ Apply try/catch/finally]
2. Java program to Find Factorial for an entered No. [ Apply try/catch/finally]
3. Java program to find largest of three numbers using ternary operator.
4. Java program to check leap year – year you have to enter using keyboard [ Apply
try/catch/finally]
5. Java Program to Reverse an input number [ Apply try/catch/finally]
6. Java Program to Calculate area of circle – Apply Method Overloading so that in one
method you have to enter the radius & value of Pi as a parameter and in the overloaded
method – only you have to enter the radius.
7. Java Program to Calculate area of triangle – apply User Defined Exception so that no –ve
value or 0 is entered as a base or height.
8. Java Program to Find sum of array elements – catch ArrayIndexOutOfBounds Exception
in this Program.
Programming With Java
End of Day 3