Given the code fragments:
andWhat is the result?
Given the code fragments:
andWhat is the result?
The code will result in a compilation error because in Java, when you override a method in a subclass, the overridden method cannot declare a broader exception than the method in the superclass. In this case, the superclass method in the Video class declares an IOException, while the subclass method in the Game class declares a broader Exception, which is not allowed. Therefore, the code will not compile.
Rule: If the superclass method declares an exception, the subclass overridden method can declare the same, subclass exception or no exception but cannot declare parent exception.
Answer B. Tested. Exception Exception is not compatible with throws clause in test.printValues()
Answer is B, tested class test{ public void printValues() throws IOException { System.out.println("Hi"); } } class test2 extends test{ public void printValues() throws Exception{ super.printValues(); System.out.println("hi2"); } }
B is true because when overriding method you must use that same Exception class or its child.
Answer is B, tested class test{ public void printValues() throws IOException { System.out.println("Hi"); } } class test2 extends test{ public void printValues() throws Exception{ super.printValues(); System.out.println("hi2"); } }
Answer is B.
The answer is A(tested) public class Video{ public void play() throws IOException{ System.out.print("Vidoe played."); } } class Game extends Video{ public void play() throws IOException{ super.play(); System.out.print("Game played."); } public static void main(String[] args) { try { new Game().play(); }catch (Exception e){ System.out.println(e.getClass()); } } }
Nevermind.
Answer is compilation error.Both classes shoult throw Exception to compile and get answer A.