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 1 of 2.
Vignesh said:
9 years ago
What is overridden?
Komal said:
3 years ago
What if object calls its own method? Please explain.
Pooja said:
7 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.
The final method can't override in subclass.
The final method can be overload.
The final variable values can't changed.
Kashi said:
8 years ago
Can you please tell higher precedence in java in case operator?
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.
Pankaj said:
9 years ago
Thank you all for explaining the concept.
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.
Ravi Kiran said:
9 years ago
Compilation fails. Final methods can't be overridden.
Sundar said:
1 decade ago
Compilation Result:
----------------------------------------
----------------------------------------
Hope this will help you. Have a nice day!
----------------------------------------
C:\Java>javac Test.java
Test.java:7: 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!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers