cancel
Showing results for 
Search instead for 
Did you mean: 

final method

Former Member
0 Kudos

hello ! i m a beginner in java and trying 4 job.Kindly help me in solving these questions.

class Base {

public final void amethod(){

System.out.println(" amethod");

}

}

public class Fin extends Base{

public static void main(String argv[]){

Base b=new Base();

b.amethod();

}

}

Options---

1. Compile time error indicating that a class with any final methods must be declared final itself .

2.Compile time error indicating that you cannot inherit from a class with final methods.

3.Run time error indicating that Base is not defined as final.

4.Success in compilation and output of "amethod" at run time.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Payal,

The answer will be 4).

In case only a method is declared "final", the restriction is that the inherited classes can not override it. I.e. if the class Fin had :

public final void amethod(){

System.out.println(" bmethod");

}

then the compilation would fail. However that's not the case and everything will run.

Answers (1)

Answers (1)

Former Member
0 Kudos

hi Payal,

Option 4 is correct. The code will successfully compile and output of "amethod" at run time.

Regards,

Richa