Home  >  Article  >  Java  >  How to define a class in java

How to define a class in java

(*-*)浩
(*-*)浩Original
2019-11-13 10:37:163832browse

Class is an important reference data type in Java and is also the basic element of Java programs, because all Java programs are based on classes. This section describes how to define classes.

How to define a class in java
#To define a class in Java, you need to use the class keyword, a custom class name and a pair of braces indicating the program body.

The complete syntax is as follows:             (Recommended learning: java course)

[public][abstract|final]class<class_name>[extends<class_name>][implements<interface_name>] {
    // 定义属性部分
    <property_type><property1>;
    <property_type><property2>;
    <property_type><property3>;
    …
    // 定义方法部分
    function1();
    function2();
    function3();
    …
}

Tips: in the above grammar , the part in the square brackets "[]" means that it can be omitted, and the vertical bar "|" means "or relationship", such as abstract|final, indicating that the abstract or final keyword can be used, but the two keywords cannot appear at the same time.

The description of each keyword in the above syntax is as follows:

public: means "shared". If public is used, it can be accessed by other classes and programs. The main class of every Java program must be a public class, and classes that serve as public tools for use by other classes and programs should be defined as public classes.

abstract: If a class is modified with abstract, the class is an abstract class. Abstract classes cannot be instantiated, but abstract classes can have abstract methods (methods modified with abstract) and concrete methods (without abstract). modification method). All subclasses that inherit this abstract class must implement all abstract methods in the abstract class (unless the subclass is also an abstract class).

final: If a class is modified with final, it is not allowed to be inherited.

class: Keyword for declaring a class.

class_name: The name of the class.

extends: Indicates inheriting other classes.

implements: Indicates the implementation of certain interfaces.

property_type: Indicates the type of member variable.

property: Indicates the member variable name.

function(): Indicates the member method name.

The above is the detailed content of How to define a class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to inherit in javaNext article:How to inherit in java