JAVA learning lesson 12th (keyword 3 final: Breaking the encapsulation of extends)

Source: Internet
Author: User

JAVA learning lesson 12th (keyword 3 final: Breaking the encapsulation of extends)

Final:

Final can modify classes, methods, and variables.

The final modified class cannot be inherited.

The final modification method cannot be overwritten.
The final variable is a constant and can only be modified once.
Internal classes can only access local variables modified by final

Disadvantages of inheritance:

The following code:

Class father {void show () {System. out. println ("ni hao"); // The method of the parent class has called the underlying System} class son extends father {void show () {System. out. println ("no"); // override of subclass 1. The original function of the parent class is suspended.} public class Main {public static void main (String [] args) {son s = new son (); s. show ();}}

Therefore, inheritance has drawbacks and breaks the encapsulation.

So for the disadvantages of inheritance, use the final keyword, final class father {}, then the father class cannot be inherited.

But if you want to inherit the class but do not want some of the methods to be inherited, you can use final to modify the method so that the method cannot be inherited.

The final modified variable, final x = 9, is changed to a constant, that is, the variable is fixed, similar to const int x = 9;

And can only be assigned once

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

Class father {void show () {final int x; // compilation failed, because no initialization exists. for Members, there is a default initialization value, while final is fixed to display the initialization value System. out. println (x );}}


Class father {void show () {final int x; x = 10; System. out. println (x); x = 10; // compilation Failed }}

Variable that cannot be modified using final, which is often used in development.

Variable writing Specification

Java writing format:

Variable: If it is composed of multiple words, the first letter of the first word is lowercase, starting from the first letter of the second word, the first letter of each word is capitalized, the writing format is the same as the function

Constant: All letters are capitalized. If the word is not unique, double MY_PI is connected to the word by an underscore;


If a value is fixed, it is unnecessary for every object to know it. This data indicates that it can be shared.

Class father {static final int x = 8; // you can use static modifier. If a member is final, static void show () {System. out. println (x );}}
In this way, it can be directly used 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 only use

class father{private static final int x = 8;   }

Global constant

class father{public static final int x = 8;   }

Why should we use final to modify the variable? If a piece of data in the program is fixed, we can use the data directly. However, the reading of the data is poor and the data needs to be written repeatedly, once you need to change the data, it is difficult to write it. Therefore, add final to give the data a name, which indicates the data, which is easy to write and easy to modify.

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.