How to use Java modifiers abstract, static and final
Modifier abstract
1. Abstract can modify a class
(1) The class modified by abstract is called an abstract class
(2) Syntax:
# Abstract class Class name {}
(3) Features: Abstract classes cannot create objects separately, but you can declare the name of
abstract category name;
(4) Abstract classes can define member variables and member methods
(5) Abstract classes have construction methods. When used to create subclass objects, jvm creates a parent class object by default;
The abstract construction method is used in Applied when jvm creates a parent class object.
2. Abstract can modify methods
(1) The method modified by asbtract is called an abstract method
(2) Syntax:
Access modification Symbol abstract Return value type method name (formal parameter list);
Note: There is no order requirement for abstract and access modifiers
(3) Features: Abstract methods only have the declaration part, no method Implementation part (not even {}, ending with;)
(4) Note: Abstract methods can only be defined in abstract classes; but abstract methods can be defined in abstract classes as well as non-abstract methods
Subclass of abstract class:
(1) Syntax:
class subclass name extends abstract class name{}
( 2) Requirement: If the subclass does not want to become an abstract class, it must cover all abstract methods in the parent class of the abstract class (purpose: to complete the abstract method implementation part);
If the subclass does not cover all abstract methods in the parent class , must be defined as an abstract class, and objects cannot be created
(3) Application: Abstract class embodies polymorphic application
Abstract class name reference name = new subclass class name () ; // The reference of the parent type stores the object of the subtype
Modifier of static? (static)
1. Static can modify attributes
( 1) Properties modified by static are called static properties, static variables, and class variables
Note: Member variables are divided into: instance variables and static variables (or static properties, class variables)
(2) Position: Definition Within the class, outside the method, it is modified by static
(3) Syntax:
Access modifier static data type variable name;
Access modifier static data type variable name=value;
Note: There is no order requirement between access modifiers and static, but they must be in front of the data type
(4) Features: Static properties exist based on classes, and how many objects are created Irrelevant, shared by all objects
(5) Use:
a. Through the object name. Static property name
b. Directly through the class name. Static property name ——>Suggestion
Note: Instance variables must be accessed through object name.Instance variable name
2. Static can modify methods
(1) Methods modified by static are called static methods
(2) Syntax:
Access modifier static Return value type method name (formal parameter list) {
// Method implementation, method body
}
Note: There is no order requirement between access modifiers and static
(3) Use:
a. Directly pass the class name. Static method name (actual parameter); --》Recommendation
b. Through object name. Static method (actual parameters); -->Not recommended
(4) Static method syntax details:
a. Only static members of this class (static properties and static methods) can be accessed in static methods )
b. Non-static members of this class cannot be directly accessed in static methods (instance variables are non-static methods)
c. The this/super keyword cannot be used in static methods
d. Static methods can be inherited by subclasses
e. Static methods can only be overridden by static methods. Static methods do not reflect polymorphic applications.
(5) Static method application scenarios: Methods in tool classes are usually set to static methods for the convenience of users.
3. Static can modify the initialization code block
(1) The initialization code block modified by static is called a static code block
(2) Static code Block position: defined within the class, the method is that {}
is modified by static class class name{
} static{
}
} }
(3) Function: When the class is loaded, the initialization of the static properties is completed in the order in which the static properties are defined
(4) Class loading:
a. Concept: jvm No. When using a class at a time, find the .class file corresponding to the class through classPath;
and the . Methods, member methods, etc.);
Save the read information to the jvm memory, and only load one class per class.
Create an object of this type at one time: complete the class loading first; then complete the creation of the object
Call the sub -class static attribute or static method
② the first creation of the sub -class object: the first class loaded, and then complete the creation of the object
load: first complete the parent class loading Class load
Create Object: First complete the creation of the parent class, the creation of the recreation of the class object
modifiers? Modified variables
final can modify variables (local variables, member variables - instance variables and static variables)
(1) Features: variables modified by final are constants within the scope, Only one assignment is allowed and can be used multiple times
Note: Once the final modified variable is assigned, it cannot be modified (2) Syntax:(3) The final-modified instance no longer has a default value, and developers have the following opportunities to assign values to it:a. Initialize it when defining and assign a value
Since # # This.a=a;
public A(int a){
#(4) Final modified static variables no longer have a default value, developers The opportunities to assign a value to it are as follows:class A{a. Initialize it when defining and assign a value
b. Initialize it using a static code block
final static int n;2. Final can modify methodsSTATIC {
n = 5;
}
}
## (5) Final modified reference, which means that the storage object in the reference cannot be changed
Can be inherited by subclasses, but subclasses are not allowed to override.
Classes modified by final cannot Inherited, that is, there are no subclasses.
The above is the detailed content of How to use Java modifiers abstract, static and final. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
