Home >Java >javaTutorial >Can the java interface be implemented?

Can the java interface be implemented?

(*-*)浩
(*-*)浩Original
2019-12-04 09:39:392335browse

Can the java interface be implemented?

Java interface definition and implementation

Use interface to define an interface. The interface definition is similar to the definition of a class, which is divided into an interface declaration and an interface body. The interface body consists of two parts: constant definition and method definition.

The basic format for defining an interface is as follows: (Recommended learning: java course)

[修饰符] interface 接口名 [extends 父接口名列表]{
    [public] [static] [final] 常量;
    [public] [abstract] 方法;
}

Modifier: optional parameter public, if omitted, It is the default access permission;

Interface name: Specifies the name of the interface. By default, the interface name must be a legal Java identifier. Under normal circumstances, the first character is required to be capitalized;

extends Parent interface name list: Optional parameter, specifying which parent interface the defined interface inherits from. When using the extends keyword, the parent interface name is a required parameter.

Method: The methods in the interface can only be defined but not implemented

For example:

public interface IStudent {
    final int X = 10;

    float getScore(int studentNo);

    int getStudentAge(int StudentNo);
}

The definition of the interface is complete, the following is the interface Implemented:

Java implements the interface, using implements:

[修饰符] class <类名> [extends 父类名] [implements 接口列表]{
}

Modifier: optional parameter, used to specify the access permissions of the class , the optional values ​​are public, abstract and final.

Class name: A required parameter, used to specify the name of the class. The class name must be a legal Java identifier. Generally, capital letters are required.

extends Parent class name: Optional parameter, used to specify which parent class the class to be defined inherits from. When using the extends keyword, the parent class name is a required parameter.

implements Interface list: Optional parameter, used to specify which interfaces this class implements. When using the implements keyword, the interface list is a required parameter. When there are multiple interface names in the interface list, separate them with commas.

public class Cire implements CalInterface   
{  
    public float getArea(float r)   
    {  
        float area=PI*r*r;//计算圆面积并赋值给变量area  
        return area;//返回计算后的圆面积  
    }  
    public float getCircumference(float r)   
    {  
        float circumference=2*PI*r;      //计算圆周长并赋值给变量circumference  
        return circumference;           //返回计算后的圆周长  
    }  
    public static void main(String[] args)   
    {  
        Cire c = new Cire();  
        float f = c.getArea(2.0f);  
        System.out.println(Float.toString(f));  
    }  
}

In class inheritance, only single inheritance can be done, but when implementing an interface, multiple interfaces can be implemented at one time, and each interface is separated by a comma ",".

At this time, constant or method name conflicts may occur. When solving this problem, if the constants conflict, you need to clearly specify the interface of the constant. This can be achieved through "interface name.constant". If a method conflict occurs, you only need to implement one method.

The above is the detailed content of Can the java interface be implemented?. 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:What is java thread poolNext article:What is java thread pool