final class Inheritance

      No Comments on final class Inheritance




Is final class Inheritance Possible??

final class in Java can not be inherited.

If you try to inherit / extend a class, it would give compile – time error. Since, final class can not be inherited , the concept of overriding is also not applicable to it.

e.g.

package com.masterjavatutorial;

public final class Employee {
	
	public final int getSalary(){
		return 20000;
	}
	 
}


package com.masterjavatutorial;
//Compile-time Error
public class Manager extends Employee{
   // Compile-time error : The type Manager cannot subclass the final class Employee 
}

Can Interface be final?

final modifier is not allowed for interfaces. Only allowed modifiers are public and abstract for interface.

Why final Class?? Where it is used??

If you want that a class sould not be subclassed , then final modifier can be used before class name. All the functionalities implemented in final class would be used as it is implemented in that class.

e.g. String is a final class. It can not be subclassed.