Final – with Inheritance

Final has three uses

1. To use as a constant variable

public class UseOfFinal {
	public static void main(String[] args) {
		final int i = 5;
		i=10; // error, because i is final
	} 
}

2. To prevent Method inheritance

class base1 {
	final void function() {
		System.out.println("inside base class");
	}
}

public class UseOfFinal1 extends base1 {
	// function() will not be inherited here
	public static void main(String[] args) {
	}
}

3. To prevent whole class inheriting

final class Base2 {
	void method() {
		System.out.println("inside method");
	}
}

public class UseOfFinal2 extends Base2 { // this is error, because by using
											// final with Base2 prevent whole
											// class inheritance
	public static void main(String[] args) {

	}
}