Java Program to Illustrate Use of Final Keyword

«
»
This is a Java Program to Illustrate Use of Final Keyword.

We have declared the length and breadth variable as final and hence its value cannot be changed now. We have made the area and rect method final so now these methods cannot be overrided. Also we have made the class containing main method as final so now this class cannot be inherited.

Here is the source code of the Java Program to Illustrate Use of Final Keyword. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. class Figure
  3. {
  4.     final int length = 5;
  5.     final int bredth = 4;
  6.     final void area()
  7.     {
  8.         int a = length * bredth;
  9.         System.out.println("Area:"+a);
  10.     }
  11. }
  12. class Rectangle extends Figure
  13. {
  14.     final void rect()
  15.     {
  16.         System.out.println("This is rectangle");
  17.     }
  18. }
  19. final public class Final_Use extends Rectangle
  20. {
  21.     public static void main(String[] args) 
  22.     {
  23.         Final_Use obj = new Final_Use();
  24.         obj.rect();
  25.         obj.area();
  26.     }
  27. }

Output:

$ javac Final_Use.java
$ java Final_Use
 
This is rectangle
Area:20

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter