Java keywords final, static, abstract class, and Interface Usage Summary

Source: Internet
Author: User
Tags java keywords

I. Final
According to the context of the program, the Java keyword final has the meaning of "This cannot be changed" or "final state". It can modify non-abstract classes, non-abstract class member methods, and variables. You may need to block changes in two ways: design or efficiency.
The final class cannot be inherited and has no subclass. The methods in the final class are final by default.
The final method cannot be overwritten by the quilt class, but can be inherited.
Final member variables indicate constants and can only be assigned once. The values do not change after the values are assigned.
Final cannot be used to modify the constructor.
Note: The Private member method of the parent class cannot be overwritten by the quilt class method. Therefore, the private type method is of the final type by default.

1. Final class
The final class cannot be inherited. Therefore, the member methods of the final class have no chance to be overwritten. By default, they are all final. When designing a class, if the class does not need a subclass, the implementation details of the class cannot be changed, and it is sure that the class will not be loaded and extended, it is designed as a final class.

2. Final Method
If a class does not allow its subclass to overwrite a method, you can declare this method as a final method.
There are two reasons for using the final method:
First, lock the method to prevent any inheritance class from modifying its meaning and implementation.
Second, efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.
For example:

Public class test1 {

Public static void main (string [] ARGs ){
// Todo automatically generates method stubs
}

Public void F1 (){
System. Out. println ("f1 ");
}
// Method that cannot be overwritten by a quilt
Public final void F2 (){
System. Out. println ("F2 ");
}

Public void F3 (){
System. Out. println ("F3 ");
}

Private void F4 (){
System. Out. println ("F4 ");
}
}

Public class Test2 extends test1 {

Public void F1 (){
System. Out. println ("test1 parent class method F1 is overwritten! ");
}

Public static void main (string [] ARGs ){
Test2 T = new Test2 ();
T. F1 ();
T. F2 (); // call the final method inherited from the parent class
T. F3 (); // call the method inherited from the parent class
// T. F4 (); // The call fails and cannot be obtained from the parent class.

}
}

3. Final variable (constant)
The final modified member variable is used to represent constants. Once given, the value cannot be changed!
Three final-modified variables are available: static variables, instance variables, and local variables, representing three types of constants respectively.
As shown in the following example, the final variable value cannot be changed once it is given.
In addition, when final variables are defined, they can be declared first without initial values. In this case, variables are also called final blank, no matter what the situation is, the compiler ensures that the blank final must be initialized before use. However, the final blank space provides greater flexibility in the use of the final keyword final. For this reason, the final data members in a class can be implemented based on objects, but there are features that keep them unchanged.

Package org. leizhimin;

Public class test3 {
Private final string S = "final instance variable s ";
Private Final int A = 100;
Public final int B = 90;

Public static final int c = 80;
Private Static final int d = 70;

Public final int e; // blank final. The initial value must be assigned when the object is initialized.

Public test3 (int x ){
E = X;
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Test3 T = new test3 (2 );
// T. A = 101; // error. The final variable value cannot be changed once it is specified.
// T. B = 91; // error. The final variable value cannot be changed once given.
// T. C = 81; // error. The final variable value cannot be changed once it is specified.
// T. D = 71; // error. The final variable value cannot be changed once it is specified.

System. Out. println (T. );
System. Out. println (T. B );
System. Out. println (T. C); // It is not recommended to access static fields using objects.
System. Out. println (T. D); // It is not recommended to access static fields using objects.
System. Out. println (test3.c );
System. Out. println (test3.d );
// System. Out. println (test3.e); // error. Because E is blank in final, it varies according to different object values.
System. Out. println (T. E );

Test3 T1 = new test3 (3 );
System. Out. println (t1.e); // The final blank variable e varies depending on the object
}

Private void test (){
System. Out. println (New test3 (1). );
System. Out. println (test3.c );
System. Out. println (test3.d );
}

Public void Test2 (){
Final int A; // blank final, assigned only when needed
Final int B = 4; // local constant -- Final is used for local variables.
Final int C; // The final is blank and has not been assigned a value.
A = 3;
// A = 4; error. The value has been assigned.
// B = 2; error. The value has been assigned.
}
}

4. Final Parameters
When the function parameter is of the final type, you can read and use this parameter, but you cannot change the value of this parameter.

Public class test4 {
Public static void main (string [] ARGs ){
New test4 (). F1 (2 );
}

Public void F1 (final int I ){
// I ++; // I is of the final type and cannot be changed.
System. Out. Print (I );
}
}

Abstract class (Abtract)
Java can define some methods without the method body. The implementation of its method body is handed over to the subclass of this class for implementation according to your own situation. Such methods are abstract methods, classes that contain abstract methods are called abstract classes.
-- Abstract classes must be modified with abstract keywords; abstract methods must also be modified with abstract. All classes in an abstract class must be abstract methods.
-- The abstract class cannot be instantiated, that is, the object cannot be generated with the new keyword.
-- Abstract methods can be declared without implementation.
-- Classes that contain abstract methods must be declared as abstract classes. subclasses of abstract classes must overwrite all abstract methods before they can be instantiated. Otherwise, this subclass is still an abstract class.
Abstract Class
{Private int I = 1; // variables can be defined in the abstract class.
Abstract int AA (int x, int y );
Abstract void BB ();
Public void CC () // you can define general methods in the abstract class.
{System. Out. println (I );}
}
Class B extends
{Int AA (int x, int y) {return 1 ;}// the method must have an inverse value.
Void BB (){}

Public static void main (string [] ARGs)
{B = new B ();
System. Out. println (B. AA (1, 2 ));
B. CC ();
}
}

Interface)
If all methods in an abstract class are abstract, we can define this class in another way, that is, interface definition. An interface is a set of definitions of abstract methods and constant values. In essence, an interface is a special abstract class that only contains the definitions of constants and methods, without the implementation of variables and methods.
-- All the members in the interface are of the public access type. The constants in the interface are identified by public static final by default. Therefore, all variables in the interface are global static constants.
-- We can define a new interface to inherit an existing interface with the extends keyword.
-- We can also define a class to use the implements keyword to implement ** all methods ** in an interface (including methods in the interface's parent interface)
---- We can also define an abstract class and use the implements keyword to implement the *** partial method *** defined in an interface ***.
-- A class can inherit a parent class and implement one or more interfaces. The extends keyword must be before the implements keyword.

Interface runner
{Public static final int id = 1; or Int id = 1 // defines a constant. Public static final can be omitted and defined as a constant, because only constants can be defined in the interface.
Void run ();
}
Interface animal extends runner // inherits the runner Interface
{Void breathe ();
}

Class fish implements animal
{Public void run (); // because the class declared in the interface is public by default, the subclass must have higher access permissions than the parent class during inheritance, therefore, you cannot directly write void run (). If you do not write the public part, an error occurs.
{System. Out. println ("fish is successful Ming ");
}
Public void breathe ()
{System. Out. println ("fish is bubbling ");
}
Public static void main (string [] ARGs)
{Fish F = new fish ();
Int J = 0;
J = runner. ID; // reference the constant ID in Runner. Because the ID is static by default, it is called directly by runner.
J = f. ID;
J = fish. ID; // because the fish class inherits the parent class, you can reference it like this.
F. ID = 2; // This error is returned because ID is a constant and cannot be assigned a value.

}
}
Abstract class landanimal implements animal // because landanimal does not inherit all methods of animal, it must be declared as an abstract class before the class.
{Public void breathe (){};//????? There is a problem. After a class is defined in this way, the object cannot be defined because it is an abstract class. However, after the class is inherited, an error occurs during compilation, and why {} is added to the abstract class {}, the original program is in my document, AAA. java
} // The problem is solved. The abstract class cannot create objects. That's right. If you inherit this class, all the previous inheritance must be overwritten, as mentioned above, {} must be added. An abstract class inherits an interface, and the method in the abstract class must be added to {}. Below is a program that inherits it, but if you define it as an abstract method, you can write it like this, Abstract Public void breathe ();
Class Lal extends landanimal
{Public void breathe (){
System. Out. println ("hhhhhhhhhhhhhhh ");
}
Public voic run () {}; // subclass inherits all methods of the parent class that are abstract classes or interfaces must inherit.
} // No problem.

Class student extends person implements runner // assume that there is a person class before, which inherits the person class and implements the implements interface. However, extends must be in front of implements.
{Public void run ();
}

Interface flyer
{Void fly ();}
Class bird implements runner, flyer // a class can inherit multiple interfaces at the same time
{Public void run (){}
Public void fly (){}
}

 

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.