Select the correct answerPredict the correct results of below given code snippet: abstract class B { abstract void funTime(); } class D extends B { final void funTime() { System.out.println("47"); } } class Main { public static void main(String args[]) { B object = new D(); object.funTime(); } }OptionsClean code with 47 printed as output.Error : overriding method is finalCode gives warning due to use of final method , but also prints 47Code ex
Question
Select the correct answerPredict the correct results of below given code snippet: abstract class B { abstract void funTime(); } class D extends B { final void funTime() { System.out.println("47"); } } class Main { public static void main(String args[]) { B object = new D(); object.funTime(); } }OptionsClean code with 47 printed as output.Error : overriding method is finalCode gives warning due to use of final method , but also prints 47Code ex
Solution
The correct answer is "Error : overriding method is final".
Explanation: In Java, the 'final' keyword when used with a method means that the method cannot be overridden in a subclass. However, in the given code, the method 'funTime()' in class 'B' is declared as abstract, which means it must be overridden in any class that extends 'B'. Therefore, declaring 'funTime()' as final in class 'D' (which extends 'B') results in a compile-time error because a final method cannot be overridden.
Similar Questions
Select the correct answerPredict the correct results of below given code snippet: abstract class B { B() { System.out.println("38"); } void funTime() { System.out.println("94"); } } class D extends B { D() { System.out.println("103"); } } class Main { public static void main(String args[]) { D object = new D(); object.funTime(); } }Options10394381033894Error: Violating concept of abstract classes38103949410338
Select the correct answerDecode the code and choose the correct results: abstract class A { final void showTime() { System.out.println("3900"); } } class B extends A { static void funTime() { System.out.println("7766"); } } public class Main { public static void main(String args[]) { A object = new B(); object.showTime(); B.funTime(); } }Options7766390077667766Error3900390039007766
Select the correct answerPredict the correct results of below given code snippet: abstract class CT { public int x; CT() { x = 39; } abstract public void showTime(); abstract final public void funTime(); } class Main extends CT { public void showTime(int a) { this.x = a; } final public void funTime() { System.out.println(x); } public static void main(String[] args) { Main object = new Main(); object.showTime(144); object.funTime(); } }OptionsError3914439144
Which option is false about the final keyword?Answer choicesSelect an optionA final method cannot be overridden in its subclasses.A final class cannot be extended.A final class cannot extend other classes.A final method can be inherited.
Analyze the following code and choose a correct answer from below. public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } } class A { int i; public void m(int i) { this.i = i; } } class B extends A { public void m(String s) { } } Group of answer choicesThe program has a compile error, because m is overridden with a different signature in B.The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.The program has a runtime error on b.i, because i is not accessible from b.The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.