Java Programming - Declarations and Access Control - Discussion


What will be the output of the program?

class A 
    final public int GetResult(int a, int b) { return 0; } 
class B extends A 
    public int GetResult(int a, int b) {return 1; } 
public class Test 
    public static void main(String args[]) 
        B b = new B(); 
        System.out.println("x = " + b.GetResult(0, 1));  

[A]. x = 0
[B]. x = 1
[C]. Compilation fails.
[D]. An exception is thrown at runtime.

Answer: Option C


The code doesn't compile because the method GetResult() in class A is final and so cannot be overridden.

Sundar said: (Jan 6, 2012)  
Compilation Result:

C:\Java>javac GetResult(int,int) in B cannot override GetResult(int,int) in A; overridden method is final.


Hope this will help you. Have a nice day!

Bhavya said: (Sep 22, 2012)  
Yes it is correct because class a is final so, it can't overridden. Thank you.

Vishal said: (Jan 15, 2013)  
final methods cannot be overridden.
final variables cannot be modified.
final class cannot be instantiated.

Coolbuddy said: (Jun 18, 2013)  
Ya it's true that if we use final then method can't be override. But in this question. Object should call it's own method.

Pranay Soundharya said: (Oct 3, 2013)  
Final methods cannot be overridden. Here overridden means same method and same signature in different subclass and superclass.

Harish said: (Mar 3, 2014)  
Final methods can't be overridden.

Abhi said: (Jun 10, 2014)  
Yes it is giving compile time error but how and why?

I know Final methods does not get inherited. In that case GetResult () in class B should be treated as its own method not as it is trying to override any method.

Anyone with clarification?

Chiranjeevi said: (Jul 5, 2014)  
Yes GetResult.... is final we cannot override that method in child class.
If variable final we cannot change value.
If class is final we cannot create child class.
If method is final we cannot override that method.

Yogesh said: (Apr 20, 2015)  
Won't method hiding will ocuur in this case?

Vignesh said: (Aug 26, 2015)  
What is overridden?

Ravi Kiran said: (Sep 28, 2015)  
Compilation fails. Final methods can't be overridden.

Hemant said: (Apr 5, 2016)  
Final method cannot be overridden.

Correct me if I am wrong, but we can rewrite the method.

Pankaj said: (May 18, 2016)  
Thank you all for explaining the concept.

Zaki said: (Feb 11, 2017)  
Method can't be overridden here as because it's a final but in super class, the value we are giving are not initialized that the reason we get compile time error. And class method treated as its own.

Kashi said: (Aug 15, 2017)  
Can you please tell higher precedence in java in case operator?

Hushen Beg said: (Aug 25, 2017)  
The final class can't be extended.
The final method can't override in subclass.
The final method can be overload.
The final variable values can't changed.

Pooja said: (Jul 24, 2018)  
Method overridden means method already in superclass same method in subclass.

Komal said: (Oct 8, 2021)  
What if object calls its own method? Please explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.