ExamVeda
Login
Home
This question belongs to Java Program Inheritence
Inheritence
?

What will be the result after compiling this code?
class SuperClass{
      public int doIt(String str, Integer... data)throws Exception{
            String signature = "(String, Integer[])";
            System.out.println(str + " " + signature);
            return 1;
      }
}

public class Test extends SuperClass{
      public int doIt(String str, Integer... data){
            String signature = "(String, Integer[])";
            System.out.println("Overridden: " + str + " " +signature);
            return 0;
      }

      public static void main(String... args){
            SuperClass sb = new Test();
            sb.doIt("hello", 3);
      }
}

Answer & Solution
Correct Answer: Option C

Exception must be caught or declared to be thrown.

Examveda
Question posted by Examveda
Community

Join the Discussion

3 Comments
Shivam Yadav
Shivam Yadav 6 years ago
doIt() method of SuperClass throws an Exception but does not catch or declare's the exception.

Rule: If you are calling a method that declares an exception, you must either caught or declare the exception.

There are two cases:

Case1:You caught the exception i.e. handle the exception using try/catch.
Case2:You declare the exception i.e. specifying throws with the method.
Maria Malik
Maria Malik 9 years ago
What exception is occurring here?
Sayeb Raheman
Sayeb Raheman 10 years ago
Why here Exception must be caught or declared to be thrown ?