Exception in Inheritance -1

Exception thrown in the parent class’s method is checked type.

If the exception is thrown by the parent’s class method then child class’s overridden method may not be required to throw the exception(not mandatory but it can throw)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Parent{
    
    void method1()  throws SQLException {
        
    }
}
 
class Child extends Parent{
    
    void method1()  {
        
    }
}
class Parent{
	
	void method1()  throws SQLException {
		
	}
}

class Child extends Parent{
	
	void method1()  {
		
	}
}


Exception thrown in the parent class’s method is unchecked type.

If the exception is thrown by the parent’s class method then child class’s overridden method may not be required to throw the exception(not mandatory but it can throw)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Parent{
    
    void method1()  throws RuntimeException {
        
    }
}
 
class Child extends Parent{
    
    void method1()  {
        
    }
}
class Parent{
	
	void method1()  throws RuntimeException {
		
	}
}

class Child extends Parent{
	
	void method1()  {
		
	}
}

Ground rule
If the exception(checked/unchecked) is thrown in the parent class’s method then child class’s overridden method is not forced to through the exception.
However it can through the exception if it wants(rules will apply).

Now lets see another scenario where only child is throwing exception but parent is not(child means here method in child class and parent means same overridden method in parent class)

Will the below program executes ?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Parent{
    
    void method1()   {
        
    }
}
 
class Child extends Parent{
    
    void method1() throws SQLException {
        
    }
}
class Parent{
	
	void method1()   {
		
	}
}

class Child extends Parent{
	
	void method1() throws SQLException {
		
	}
}


It will fail at compile time.

The reason is it is throwing checked exception.

Ground rule: if the child is throwing any checked exception then parent must also through the exception otherwise compilation fails.

Now see the below program

1
2
3
4
5
6
7
8
9
10
11
12
13
class Parent{
    
    void method1()   {
        
    }
}
 
class Child extends Parent{
    
    void method1() throws RuntimeException {
        
    }
}
class Parent{
	
	void method1()   {
		
	}
}

class Child extends Parent{
	
	void method1() throws RuntimeException {
		
	}
}


Compilation success ?

Yes it is success because it is run time exception
Ground rule:if the child is throwing any unchecked exception then parent need not to throw exception.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on