zoukankan      html  css  js  c++  java
  • Summary: Final Keyword

    In this tutorial we will learn the usage of final keyword. final keyword can be used along with variables, methods and classes. We will cover following topics in detail.

    1) final variable
    2) final method
    3) final class

    1. Final Variables

    final variables are nothing but constants. We cannot change the value of a final variable once it is initialized. Lets have a look at the below code:

    class Demo{  
    
       final int MAX_VALUE=99;
       void myMethod(){  
          MAX_VALUE=101;
       }  
       public static void main(String args[]){  
          Demo obj=new  Demo();  
          obj.myMethod();  
       }  
    }

    Output:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The final field Demo.MAX_VALUE cannot be assigned
    
    	at beginnersbook.com.Demo.myMethod(Details.java:6)
    	at beginnersbook.com.Demo.main(Details.java:10)

    2. Final Methods

    A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it.

    final modifier indicates that the method may not be overridden in a derived class.

    3. Final Class

    We cannot extend a final class. 

    When applied to a class, the final modifier indicates that the class can not be used as a base class to derive any class from it. 

  • 相关阅读:
    CodeForces 670E Correct Bracket Sequence Editor
    CodeForces 670F Restore a Number
    HDU 5895 Mathematician QSC
    HDU 5880 Family View
    HDU 5886 Tower Defence
    CSS3.16
    CSS页面定制代码+动漫人物设计
    TSP变形(三进制状压)
    三进制状压(涂抹果酱)
    最小点覆盖(König定理)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5141896.html
Copyright © 2011-2022 走看看