Java Impo
Java Impo
Exceptional Handling
Types of Error:
1. Compile time error – Syntax Error
2. Run-Time error – Logical Error
Definition of Exception
1. An Exception is condition that is caused by run-time error in
program.
2. Exception Handling in Java is one of the effective means
to handle runtime errors so that the regular flow of the
application can be preserved
Major reasons why an exception Occurs
• Invalid user input
• Device failure
• Loss of network connection
• Physical limitations (out-of-disk memory)
• Code errors
• Opening an unavailable file
1. printStackTrace()
This method prints exception information in the format of the Name of the
exception: description of the exception, stack trace.
import java.io.*;
class GFG {
public static void main (String[] args) {
int a=5;
int b=0;
try{
System.out.println(a/b);
}
catch(ArithmeticException e){
e.printStackTrace();
}
}
}
Features of Exception
1. Find the problem( Hit the exception )
2. Inform that an error has occurred ( throws Exception )
3. Receives the error information ( Catch exception )
4. Take corrective actions (handle the Exception)
try {
catch(Exception e) {
Example
public class Main {
try {
} catch (Exception e) {
Java Arrays
Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
String[] cars;
Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
CHANGE ELEMENT IN ARRAY:
Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
ARRAY LENGTH:
Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4
CONSTRUCTOR:
Meaning:
A constructor in Java is a special type of method that is called
when an object of a class is created. It is used to initialize the
object's state.
Syntax:
The syntax of a constructor in Java is:
public ClassName()
{
// constructor body
}
Rules to Create a Constructor:
1. The constructor method must have the same name as the
class.
2. Constructors do not have a return type, not even void.
3. Constructors can be overloaded, meaning a class can have
multiple constructors with different parameters.
4. If you don't define any constructor in your class, Java
provides a default constructor (without parameters)
automatically.
Types:
There are mainly two types of constructors:
1. Default Constructor: Constructor without any parameters.
2. Parameterized Constructor: Constructor with parameters.
EXAMPLE – note book
VISIBILITY CONTROL:
Visibility control in Java refers to the ability to control the access levels of classes, fields,
methods, and constructors. This is crucial for encapsulation and data hiding in object-
oriented programming.
Meaning:
Visibility control defines who can access the members (fields, methods, constructors) of
a class.
Uses:
1. Encapsulation: It allows you to hide the internal state of an object and only
expose what is necessary, improving the maintainability and robustness of code.
2. Access Control: It prevents unauthorized access and manipulation of sensitive
data.
3. Modularity: By controlling visibility, you can control how different components
of your program interact, enhancing modularity.
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
How to compile java package
If you are not using any IDE, you need to follow the syntax given below:
For example
1. javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file. You can use
any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you
want to keep the package within the same directory, you can use . (dot).
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
Output:Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destination.
How to access package from another package?
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Method Overriding:
Meaning:
Method overriding in Java occurs when a subclass provides a
specific implementation of a method that is already provided
by its superclass.
SYNTAX
@Override
<access_modifier> <return_type>
methodName(<parameter_list>) {
// method body
}
EXAMPLE:
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
OUTPUT:
Animal makes a sound
Dog barks
Method Overloading:
Meaning:
Method overloading in Java allows a class to have multiple
methods with the same name but with different parameters.
SYNTAX:
<access_modifier> <return_type>
methodName(<parameter_list>) {
// method body
}
// Example of overloading:
<access_modifier> <return_type>
methodName(<parameter_list_1>) {
// method body
}
<access_modifier> <return_type>
methodName(<parameter_list_2>) {
// method body
}
EXAMPLE:
// Overloaded method
public double add(double a, double b) {
return a + b;
}
OUTPUT:
Result 1: 5
Result 2: 6.0