Java Interview Question #3 : Can Java have blank final Value ?

Que 3. Can Java have blank final Value ?

Answer : No

package com.c4learn.inheritance;
public class BlankFinalValue {
    final int myValue;
    public BlankFinalValue(){
    	this.myValue = 3;
    }
    public static void main(String[] args) {
    	BlankFinalValue s1 = new  BlankFinalValue();
    }
}

Output :

Compiler Successfully

Explanation :

  1. We cannot have final Value which is uninitialized.
  2. You will get compile time error if you did not provide initialization.
  3. In this case we have to provide initialization in the constructor. (As shown in above program)

If final variable is not initialized in the constructor then it will throw compile time error as below -

Uninitialized Final Value

If You have Multiple Constructor then -

we need to initialize final variable in each of the constructor like below program -

package com.c4learn.inheritance;
public class BlankFinalValue {
    final int myValue;
    public BlankFinalValue(){
    	this.myValue = 3;
    }
    public BlankFinalValue(int num){
    	this.myValue = num;
    }
    public static void main(String[] args) {
    	BlankFinalValue s1 = new  BlankFinalValue();
    }
}

If you failed to initialize final variable in any of the constructor then you will get compile time error -

The blank final field myValue may not have been initialized

Java Interview Question #2 : Can Java inherit Final Method ?

Que 2. Can we inherit Final Method ?

Answer : Yes

In case of final method, Method is inherited but cannot be overriden so always method from parent class will be executed.

Consider the following example -

package com.c4learn.inheritance;
public class ShapeClass {
    final void setShape() {
    	System.out.println("I am Inherited");;   	
    }
    public static void main(String[] args) {
    	Circle c1 = new  Circle();
    	c1.setShape();
    }
}
class Circle extends ShapeClass {
}

Output :

I am Inherited

We can inherit the final method but cannot override the final method inside the child class.

Java Interview Question #1 : Is Final Method more efficient in Java ?

Que 1. Final Vs Non Final Method, Which is more efficient ?

Answer : Final

  1. Final methods execute more efficiently than non final method.
  2. Compiler knows at compile time that a call to a final method won’t be overridden by some other method

Let’s See, How ?

Consider the final method,

package com.c4learn.inheritance;
public class ShapeClass {
    final void setShape() {
    	System.out.println("I am Inherited");;   	
    }
    public static void main(String[] args) {
    	Circle c1 = new  Circle();
    	c1.setShape();
    }
}
class Circle extends ShapeClass {
}

Whenever we create an object of the child class then compiler will check whether method written inside the parent class is overriden or not at run time.

final void setShape() {
    System.out.println("I am Inherited");;   	
}

In this case compiler already knows that, method is declared as final so it does not waste time in checking whether method is final or not.