Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 1)
1.
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));  
    } 
}
x = 0
x = 1
Compilation fails.
An exception is thrown at runtime.
Answer: Option
Explanation:

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

Discussion:
18 comments Page 1 of 2.

Komal said:   3 years ago
What if object calls its own method? Please explain.

Pooja said:   6 years ago
Method overridden means method already in superclass same method in subclass.

Hushen Beg said:   7 years ago
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.

Kashi said:   7 years ago
Can you please tell higher precedence in java in case operator?

Zaki said:   7 years ago
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.

Pankaj said:   8 years ago
Thank you all for explaining the concept.

Hemant said:   8 years ago
Final method cannot be overridden.

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

Ravi Kiran said:   9 years ago
Compilation fails. Final methods can't be overridden.

Vignesh said:   9 years ago
What is overridden?

Yogesh said:   9 years ago
Won't method hiding will ocuur in this case?


Post your comments here:

Your comments will be displayed after verification.