javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



final method in java

  • The final method cannot be overridden by the subclass.
  • The final method can not be modified from the subclass.

Example

 class  Examfinal {
	 public  final void   display() {
		System.out.println("Welcome Learner" );
	}
}

 public class   test1  extends  Examfinal {
	
	// trying to change display() method, but it will give error message
	 public void  display() {
		System.out.println("Hello World");
	}
	 public static void  main(String[] args) {
		test1 t1 =  new  test1();
		t1.display();
	}
}

output

/test1.java:10: error: display() in test1 cannot override display() in Examfinal 
public void display() {
^
overridden method is final
1 error



© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.