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));
}
}
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 2 of 2.
Ravi Kiran said:
9 years ago
Compilation fails. Final methods can't be overridden.
Hemant said:
9 years ago
Final method cannot be overridden.
Correct me if I am wrong, but we can rewrite the method.
Correct me if I am wrong, but we can rewrite the method.
Pankaj said:
9 years ago
Thank you all for explaining the concept.
Zaki said:
8 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.
Kashi said:
8 years ago
Can you please tell higher precedence in java in case operator?
Hushen Beg said:
8 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.
The final method can't override in subclass.
The final method can be overload.
The final variable values can't changed.
Pooja said:
7 years ago
Method overridden means method already in superclass same method in subclass.
Komal said:
3 years ago
What if object calls its own method? Please explain.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers