Definition and implementation of interfaces in Java

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, which specifies the access permission 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 parent Interface Name list: An optional number that specifies 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.

For example, define an interface for calculations, where a constant pi and two methods are defined, with 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:
As with 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 : An optional number that specifies 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 multiple interface names exist 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 parameters and the type must be fully consistent with the interface, and all methods in the interface must be implemented. For example, write a class called Cire, which implements the interface calculate defined in section 5.7.1, with 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));}    }
In the class inheritance, only can do single-inheritance, while implementing the interface, one can implement more than one interface, each interface using a comma "," separated. In this case, a constant or method name conflict may occur, when solving the problem, assuming constant conflict, you need to understand the interface of the specified constant, which can be achieved through the "interface name. Constant" Implementation. Assuming a method conflict occurs, it is possible to implement only one method. The following is a concrete example of how to solve the above problem.

Definition and implementation of interfaces in Java

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.