Java_BSc(H)_Unit-3
Java_BSc(H)_Unit-3
There can be only abstract methods in the Java interface, not method body. It is used to
achieve abstraction and multiple inheritance in Java
.
In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
Syntax:
interface <interface_name>{
interface printable
{
public abstract void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A6 obj = new A6();
obj.print();
}
}
Output:
Hello
Example
interface Printable
{
public abstract void print();
}
interface Showable
{
public abstract void show();
}
class A7 implements Printable,Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[])
{
A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:Hello
Welcome
****************************************************************
Benefits:
1. The classes contained in the packages of other program can be easily reused.
2. In packages classes are unique. They are compares with classes in other packages. i.e.
two classes in two different packages can have the same name that may refer by their
fully qualified name, comprising the package name and the class name.
3. Packages provide a way to hide classes. They prevent other programs or packages from
accessing classes that are meant for internal use only.
Java packages are classified into two types.
1. Java API packages (Pre defined packages).
2. User defined packages.
Java
A package is to be included in the user application so that the predefined classes can be used
to achieve this. One can use a keyword called “import”.
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
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.
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but
not subpackages.
The import keyword is used to make the classes and interface of another package accessible
to the current package.
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
*************************************************************************
**
class MisspelledVar {
public static void main(String args[])
{
int a = 40, b = 60;
3.Logical Error:
A logic error is when your program compiles and executes, but does the wrong thing or
returns an incorrect result or no output when it should be returning an output. These errors
are detected neither by the compiler nor by JVM. The Java system has no idea what your
program is supposed to do, so it provides no additional information to help you find the
error. Logical errors are also called Semantic Errors.
1. Built-in Exceptions
o Checked Exception
o Unchecked Exception
2. User-Defined Exceptions
Built-in Exception
Exceptions that are already available in Java libraries are referred to as built-in exception.
These exceptions are able to define the error situation so that we can understand the reason
of getting this error. It can be categorized into two broad categories, i.e., checked
exceptions and unchecked exception.
Checked Exception
Checked exceptions are called compile-time exceptions because these exceptions are
checked at compile-time by the compiler. The compiler ensures whether the programmer
handles the exception or not. The programmer should have to handle the exception;
otherwise, the system has shown a compilation error.
Unchecked Exceptions
The unchecked exceptions are just opposite to the checked exceptions. The compiler will
not check these exceptions at compile time. In simple words, if a program throws an
unchecked exception, and even if we didn't handle or declare it, the program would not give
a compilation error. Usually, it occurs when the user provides bad data during the interaction
with the program.
User-defined Exception
In Java,we already have some built-in exception classes
like ArrayIndexOutOfBoundsException,NullPointerException,and ArithmeticExceptio
n. These exceptions are restricted to trigger on some predefined conditions. In Java, we can
write our own exception class by extends the Exception class. We can throw our own
exception on a particular condition using the throw keyword. For creating a user-defined
exception, we should have basic knowledge of the try-catch block and throw keyword.
Exception Handling
Java provides specific keywords for exception handling purposes.
try
catch
finally
throw
throws
try: It is a keyword used to create block of statements which are doubtful of generating
exception.
Syntax:
try
Doubtful code;
Syntax:
Handling code;
Example
import java.lang.*;
class Excep
int x=10,y=0,z;
try
z=x/y;
System.out.println(z);
catch(ArithmeticException e)
System.out.println(“Progfram over”);
throw: It is a keyword used to create an exception and throw it explicitly. Throw our
own exception using throw keyword. The flow of execution stops immediately after the
throw statements and any subsequent statements are not executed.
Syntax
throw new ExceptionType(“content”);
Example:
import java.lang.*;
class Simple
try
catch(ArithmeticException e)
System.out.println(e.getmessage());
throws: It is a keyword, it contains list the type of exceptions that a method may throw.
throws keyword is used when we don’t want to handle the exception and try to send the
exception to the JVM.
Syntax:
returntype method name(parameters) throws exception list
---------
--------
Example:
import java.lang.*;
class Simple
int x=10;
int y=0;
int res=x/y;
System.out.println(res);
try
Msg();
catch(ArithmeticException e)
finally: It is a keyword used to create a block of code that will be executed after a
try/catch block whether exception is handle or not.
If an exception occurs then the catch block executes after that finally block will be
executed.
In case there is an exception, but there is no catch block, then the code execution skips the
rest of code and executes the finally block.
Syntax:
1. try
Doubtful code;
finally
Executable code;
2. try
Doubtful code;
catch(ExceptionType object)
Handling code;
finally
Executable code;
Example:
import java.lang.*;
class Excep
try
int x=10,y=0,z;
z=x/y;
System.out.println(z);
catch(ArithmeticException e)
Finally