Exception in inheritance -2

Before understanding the below article be sure with a previous article, So that it helps you to understand.

Based on previous post ,observe the below program

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() throws RuntimeException {
        
    }
}
class Parent{
	
	void method1() throws SQLException  {
		
	}
}

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


Will it compile successfully ?

Yes.. yes.. yes..

Apply previous post’s ground rules

1) If parent is throwing any exception then child may not be required to throw exception(but it can throw)    
      
    satisfied

2) If the child is throwing any unchecked exception then parent need not to throw exception(but it can throw)  
        
    satisfied


Hence above program compiles successfully.

Now see the below program(just revrse of above program)

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() throws SQLException {
        
    }
}
class Parent{
	
	void method1() throws RuntimeException  {
		
	}
}

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


will it compiles successfully ?

Yes, Are you sure ? yes, Are you sure ? yes, think

Lets apply the common ground rules

1) If parent is throwing any exception then child may not be required to throw exception(but it can throw)   
      
     satisfied

2) If the child is throwing any unchecked exception then parent need not to throw exception(but it can throw)


Since it is pointing out child should throw unchecked exception to have this ground rule

But in above program child is throwing checked exception, oh then what happens ?

Above program compilation fails fails…? 100% yes it fails.

Ground rule : If the child is throwing any checked exception then parent must throw only checked exception.

Then will the below program compiles successfully?

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

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


No the rule is if child is throwing any checked exception then parent must throw a)same checked exception or b)super type of that checked exception

So below program is the best example of this

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() throws  SQLDataException {
        
    }
}
class Parent{
	
	void method1() throws SQLException  {
		
	}
}

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

Ground rule : If the child is throwing any checked exception then parent must throw only checked exception and it should satisfy the above condition

a)child should throw same exception as parent or

b)child should throw any subclass exception of the parent.

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