logow

Java Exception Handling And Abstraction Class And Interface With Example Program

HELLO WELCOME TO THIS JAVA  BLOG...........


IN THIS TUTORIAL ALL THESE THINGS ARE COVER........

ABSTRACTION CLASS AND METHODS AND ALSO INTERFACE WITH EASY EXAMPLE ................

1.) WHAT IS JAVA EXCEPTION HANDLING AND EXCEPTION HANDLING TECHNIQUES LIKE TRY , CATCH , THROW , THROWS , FINALLY ETC.........


Java
Java 



JAVA EXCEPTION


An Exception is an unwanted event that interrupts the normal flow of the program. When an exception occurs program execution gets terminated. In such cases we get a system generated error message. The good thing about exceptions is that they can be handled in Java. By handling the exceptions we can provide a meaningful message to the user about the issue rather than a system generated message, which may not be understandable to a user.There are java exception handling techniques that are used to help to recover from java application crash.it is similar like if-else method when if false then else part will be executed . In this when try  false then catch throws warning not crash occur . For Example : 100/0 not possible but catch function avoid it.......

TYPES OF EXCEPTION HANDLING IN JAVA ......
There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as the unchecked exception. According to Oracle, there are three types of exceptions In JAVA......

1.) Checked Exception
2.) Unchecked Exception
3.) Error

1) Checked Exception
The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError and many more etc..

PROGRAM FOR JAVA EXCEPTION HANDLING ........

public class JavaExceptionExample{  
  public static void main(String args[]){  
   try{  
      
      int data=10/0;  
   }catch(ArithmeticException e){System.out.println(e);}  
   
   System.out.println("rest of the code...");  
  }  
}  



OUTPUT IS -----
rest of the code 

KEYWORD IN JAVA USING EXCEPTION HANDLING ........

1.) try-----> The "try" keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can't use try block alone.

2.) catch-----> The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.

3.) finally------> The "finally" block is used to execute the important code of the program. It is executed whether an exception is handled or not.
throw The "throw" keyword is used to throw an exception.

4.) throws------> The "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.


2.) WHAT IS JAVA ABSTRACTION CLASS AND INTERFACE IN JAVA ?????? 


ABSTRACTION CLASS AND METHODS WITH PROGRAMS EXAMPLE------

Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides Implementation details to the user.

When a class declare with keyword abstract it is known as abstract class in java .

A abstract class has no object to be created.
a abstract class has both abstract and non-abstract methods and also constructor and class data members....

Here we take a Example of abstract class ........


abstract class hero{
  abstract void show();
}
class cricketer extends hero{
void show()
{
System.out.println("sharukh is my favourite hero");
}
public static void main(String args[])
{
 hero obj = new cricketer();
 obj.show();
}
}  

OUTPUT IS -----

sharukh is my favourite hero  ......

INTERFACE IN JAVA PROGRAMMING ...........

Interface in java like a abstract class but interface does not support contains constructor but it is also support Multiple constructor as well as Compare to Abstract class .........

Members of interface cannot be static .

Members of Abstract class can be static . 

 Interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

Java Interface also represents the IS-A relationship

JAVA INTERFACE EXAMPLE WITH SOURCE CODE .....

interface Polytechnic{  
void results();  
}  
class topper implements Polytechnic
{  
public void results()
{
System.out.println("Results declare ");

}  
  
public static void main(String args[]){  
Polytechnic cse4th = topper();  
object.results();
 }  
}  

OUTPUT IS ------
Results declare ----------



THANKS !!!!!!!!!!!!!!!!



Post a Comment

0 Comments