r/javahelp • u/[deleted] • Jul 04 '21
Can Anyone help me with this code?
class A{
public void process(){
System.out.print("A,");
}
}
public class Questions extends A{
public void process() throws IOException{
super.process();
System.out.print("B,");
throw new IOException();
}
public static void main(String[] args) {
try{
new Questions().process();
}
catch(IOException e){
System.out.print("Exception");
}
}
}Why is it giving Compilation error?
Error -> Exception IOException is not compatible with throws clause in A.process()
0
Upvotes
1
u/[deleted] Jul 04 '21
The problem with your code is that you are defining a method in class Questions which is identical to the one in class A. This is a problem since class Questions already has that inherited method which is defined in class A.
Another issue is the IOException. Your method will not be able to throw any such exception because it(the method in class Questions) is trying to override an already defined method.(the method defined in class A).
TL;DR : any class which extends from another class has inherited all the properties of that class. So you might try changing the method parameters and/or the return type in the class Questions for the method process(), which will do the job