Java Learning lesson 12th (keyword Three final: breaking encapsulation for extends)

Source: Internet
Author: User

Final

Final can modify classes, methods, variables

The final decorated class cannot be inherited

The final modified method cannot be overridden
The final modified variable is a constant that can only be decorated once
Inner classes can only access the final decorated local variables

Disadvantages of Inheritance:

The following code:

Class Father{void Show () {System.out.println ("Ni hao");//The method of the parent class has already called the underlying system}}class son extends Father{void show () { System.out.println ("no");//Sub-Class A cover, the original function of the parent class hangs}}public class Main{public static void Main (string[] args) {son s = new Son (); s . Show ();}}

Therefore: The inheritance has the disadvantage, it breaks the encapsulation sex.

So for the disadvantage of inheritance, using the final keyword, final class father{}, then the Father class can not be inherited

However, if you want to inherit the class, but do not want some of the methods to be inherited, then use the final modification method, then the method cannot be inherited

And the final modified variable, final x = 9 becomes a constant, which means that the variable is fixed, similar to the const int x = 9;

And can only be assigned one time

Class father{   void Show () {   final int x = 9;   x = 10;//Compilation failed System.out.println (x);}}

Class father{   void Show () <span style= "White-space:pre" ></span>{final int x;//compilation failed because there is no initialization, for members, There is a default initialization value, and final fixed is the display initialization value SYSTEM.OUT.PRINTLN (x);}}


Class father{   void Show () {   final int x;   x = 10; SYSTEM.OUT.PRINTLN (x); x = 10;//compilation Failed}}

A variable that is not changed by final modification, which is commonly used in development

Writing Specifications for variables

Writing format in Java:

variable: If it consists of multiple words, the first letter is lowercase, starting with the first letter of the second word, the first letter of each word is capitalized, and the writing format and function are the same

Constants: All letters are capitalized, and if the word is not unique, the word is underlined with the word double my_pi;


If a value is fixed, there is no need for each object to know, then this data indicates that it can be shared

Class Father{static final int x = 8;//so you can use the static modifier, and if the member is final, it will generally add static   void Show () {   System.out.println (x );}   }
This can be used directly by the class name
Class Father{static Final int x = 8;   } public class Main{public static void Main (string[] args) {System.out.print (father.x);}}

If you use only in this class

Class Father{private static final int x = 8;   }

Global constants

Class Father{public static final int x = 8;   }

Why use final modified variables, in the program if a data is fixed, then the direct use of this data can be, but this data is poor reading, the data also have to write repeatedly, once the data to change, the writing is more troublesome , so add final to this data to a name, This name represents this data, easy to write, easy to modify

Java Learning lesson 12th (keyword Three final: breaking encapsulation for extends)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.