Menu
Topics Index
...
`

Final keyword950


If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword950
What will be the output of the following program?
class Result
{
    public static void main(String args[])
    {
        B b = new B();
        System.out.println("x = " + b.getResult(0, 1));  
    }
}

class A
{
    final public int getResult(int a, int b) { return a * b; }
}

class B extends A
{
    public int getResult(int a, int b) { return a + b; }
}

A. x = 1
B. x = 0
C. Compilation Error
D. Runtime Error
Topic: final Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C..here we overriding concept where child class redefine the method in the Parent class..overriding occurs when inheritance is involved, in overriding the return type,name,num and order and type of arg must be same
Execution:
here class A is the Parent class and class B is Child class,in the Parent class we have the getResult() with the 2 arg and we are  returning the int value..in the Child class we are overriding the same methodh which returns the sum of the arg but here compiler will raise an error Bcz getResult() is defined as final in the class A so we can't override it in the Child class

Posted by Uday Kumar    2015-03-04 08:40:27


Ans is compilation error since we cannot override the final method getResult in class A into class B

we get error at line.....//

public int getResult(int a,int b) and raises an error as overridden method is final

Posted by Vicky Maduri    2015-03-04 08:47:20


ANS IS C (Compilation Error)

--Here,the concept of "Method Overriding" keyword is used.

--Here,Three classes are defined:
1.Result
2.A
3.B (it extends class A)

--Inside main() method, object of class B() is created.After that getResult() method is called by passing 0 and 1.

--Inside class A getResult() method is defined as final inside which logic of multiplication is written.

--Final Keyword:
In java final keyword has 3 uses:
1.Declare Constants
2.Prevent methods from getting overridden
3.Prevent classes from getting extended.

As here,getResult() method is declared as final so it can not be overridden.But in class B attempt is made to override this method which gives compilation Error.

--After that,class B is defined which extends the class A and getResult() method is overriden inside which logic of addition is written.

Posted by Mânïshå Mùlchåndânï    2015-03-04 14:29:27


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Vicky Maduri,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Switguy Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-05 01:28:56


© meritcampus 2019

All Rights Reserved.

Open In App