CodesJava

Easy learning with example program codes

what is final class in java?


final class in java

A class declared with final keyword is known as final class. A final class can’t be inherited.

Example

/**
 * This program is used to show that final class can't be inherited.
 * @author CodesJava
 */
final class Show{
	public void show(){
		System.out.println("Hello world.");
	}
}
//error because final class can't be inherited.
class Display extends Show{
	public void display(){
		System.out.println("Hello codesjava.com.");
	}
}
 
public class FinalExample3 {
	public static void main(String args[]){
		//creating object of Display class
		Display obj = new Display();
		//method call
		obj.display();
	}
}

Output

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The type Display cannot subclass the final class Show
at com.codesjava.business.Display.(FinalExample3.java:13)
at com.codesjava.business.FinalExample3.main
(FinalExample3.java:22)

Java interview questions on final keyword

Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki