wellnwill Marathahalli Bangalore

Overriding Concept with Exception Handling

Case 1:

Parent class method is not declaring an exception

Then

We cannot declare the checked exception with child class overridden method. We can declare the unchecked exception with child class overridden method.

import java.io.*; 
class ExcepTest {
                void lifeStyle ()
		{
			System.out.println("eat,work,sleep");
		}
}
class ChildExcepTest extends ExcepTest
{
	void lifeStyle () throws FileNotFoundException  //checked Exception is not allowed
		{
			System.out.println("eat,work,sleep ,play,party");
		}
	
	public static void main(String args[])
	{  
		ExcepTest e1=new ChildExcepTest();  
		   e1.lifeStyle(); 
              }
}
//O/P-CTE

import java.io.*; 
class ExcepTest1 {
	
              void lifeStyle ()
		{
			System.out.println("eat,work,sleep");
		}
}

class ChildExcepTest1 extends ExcepTest1
{
	void lifeStyle() throws ArithmeticException//unchecked Exception is allowed
		{
			System.out.println("eat,work,sleep ,play,party");
		}	
	public static void main(String args[])
	{  
		ExcepTest1 e1=new ChildExcepTest1();  
		   e1.lifeStyle(); 
             }
}
//O/P-CTS

Checked Exception:


IOException (Parent class)

a. FileNotFoundException(child class)

b. EOFException(child class)


Case 2:

Parent class method is declaring checked exception

Then

Child class method can throw the same checked exception or its child class exception.

Child class method can throw unchecked exception.

We can ignore declaring an exception with child class overridden method.

Child class method cannot throw the exception which is parent class exception of declared method in parent class.

Child class method cannot throw the exception which is non parent class exception.

import java.io.*; 
class ExcepTest3 {
	void m1() throws IOException
		{
			System.out.println("Hi");
		}
}
class ChildExcepTest3 extends ExcepTest3
{
	void m1() throws IOException,FileNotFoundException ,ArithmeticException 
                        //can throw same or child checked exception or any unchecked exception
		{
			System.out.println("Hi");
		}
	
	public static void main(String args[])
	{  
		ExcepTest3 e1=new ChildExcepTest3(); 
    try{		
		   e1.m1(); 
	}
	catch(Throwable e)
	{
		System.out.println(e);
	}
}
}
//O/P-CTS
import java.io.*; 
class ExcepTest4 {
	void m1() throws IOException
		{
			System.out.println("Hi");
		}
}
class ChildExcepTest4 extends ExcepTest4
{
	void m1() throws Exception //Super class of child exception is not allowed
		{
			System.out.println("Hi");
		}
	
	public static void main(String args[])
	{  
		ExcepTest4 e1=new ChildExcepTest4(); 
        try{		
		   e1.m1(); 
	}
	catch(Throwable e)
	{
		System.out.println(e);
	}
}
}

import java.io.*; 
class ExcepTest5 {
	void m1() throws IOException
		{
			System.out.println("Hi");
		}
}
class ChildExcepTest5 extends ExcepTest5
{
	void m1() throws ClassNotFoundException
		{
			System.out.println("Hi");
		}
	public static void main(String args[])
	{  
		ExcepTest5 e1=new ChildExcepTest5(); 
                     try{		
		   e1.m1(); 
	            }
	catch(Throwable e)
	 {
		System.out.println(e);
	 }
}
}
//O/P-CTE
Note- Child class method cannot throw the exception which is non parent class exception.

Case 3:

Parent class method is declaring unchecked exception.

Then

Child class method cannot throw the checked exception. Child class method can throw same or any unchecked exception.

We can ignore declaring an exception with child class overridden method.

Footer with Map