Java Definition and implementation interface

Source: Internet
Author: User
Tags constant definition

1. Define the interface
Use interface to define an interface.

Interface definition similar to the definition of the same, is also divided into the interface of the Declaration and interface body, which consists of a constant definition and method definition two parts. The basic format for defining an interface is as follows:

[Modifier] Interface Interface Name [extends parent interface List]{

[Public ] [static] [final] constant;
[public] [abstract] method;
}
Modifier: Optional. Lets you specify access permissions for the interface, and the optional value is public. If omitted, the default access permission is used.


Interface Name: A required parameter that specifies the name of the interface, which must be a valid Java identifier. Under normal circumstances, the first letter is required to be capitalized.
Extends list of parent interface names: Optional parameters. Used to specify which parent interface the interface to define inherits from. When Extendskeyword is used, the parent interface name is the required number of parameters.


Method: The methods in the interface are only defined and not implemented.


Like what. Defines an interface for computation, in which a constant pi and two methods are defined. Detailed code such as the following:

Public interface Calinterface {    final float pi=3.14159f;//defines the constant Pi    float Getarea (float R) used to represent PI;// Defines a method for calculating the area getarea ()    float getcircumference (float R);//defines a method for calculating the perimeter getcircumference ()}
Attention:
The same as the Java class file. The file name of the interface file must be the same as the interface name.
Implementing interfaces
Once an interface is defined, it is able to implement that interface in the class. Implementing an interface in a class can use Keywordimplements, whose basic format is as follows:
[Modifier] Class < class name > [extends parent class name] [Implements interface list]{
}
modifier:An optional number that specifies the access permission for the class, with the optional values public, abstract, and final.
class Name:A required parameter that specifies the name of the class and the class name must be a valid Java identifier. Under normal circumstances, the first letter is required to be capitalized.
extends parent class name: Optional number of references. Used to specify which parent class the class to define inherits from. When Extendskeyword is used. The parent class name is a required number of parameters.


implements interface list: An optional number that specifies which interfaces the class implements. When using Implementskeyword, the interface list is a required number of parameters. When there are multiple interface names in the interface list. Each interface name is separated by commas.


When implementing an interface in a class, the name of the method, the type of return value, the number of arguments, and the type must be fully consistent with the interface. And all the methods in the interface must be implemented. For example, write a class called Cire. This class implements the interface calculate defined in section 5.7.1. Detailed code such as the following:

public class Cire implements Calinterface {public    float Getarea (float r)     {        float area=pi*r*r;// Calculates the circle area and assigns a value to        the variable areas return area;//returns the calculated Circle area. public    Float getcircumference (float r)     {        float Circumference=2*pi*r;      Calculates the circumference of a circle and assigns a value to the variable circumference        return circumference;           Returns the computed circumference long    } public    static void Main (string[] args)     {        Cire c = new Cire ();        float f = c.getarea (2.0f);        System.out.println (Float.tostring (f));}    }
Inherits the class. Just do single inheritance, and implement interfaces. Once you are able to implement multiple interfaces, use a comma "," separating each interface. In this scenario, the name of the constant or method may conflict and resolve the issue. Assuming the conflict is constant, we need to know the constants of the specified interface, so that the "interface name. Unchanged" Implementation can be implemented. How the conflicting assumptions occur when the. It is just a method that can be implemented. The following methods are specific examples of the specific issues that are addressed above.

Java Definition and implementation interface

Related Article

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.