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, where the interface body 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, used to specify access permissions for the interface, and the optional value is public. If omitted, the default access rights are used.
Interface Name: A required parameter that specifies the name of the interface and the interface name must be a valid Java identifier. In general, the first letter is required to capitalize.
Extends parent interface Name list: Optional parameter that specifies which parent interface the interface to define inherits from. When you use the extends keyword, the parent interface name is a required parameter.
Method: The method in the interface is only defined and not implemented.

For example, define an interface for calculations that defines a constant pi and two methods in that interface, with the following code:

/span>

    1. public   interface  calinterface   

    2. {  

    3.     final  float  pi=3 . 14159f; //defines the constant pi    used to represent pi;

    4.      float  getarea (float  r); //defines a method for calculating the area getarea ()   

    5.     float  getcircumference (float  r); //defines a method for calculating the perimeter getcircumference ()   

    6. }  

Attention:
As with Java class files, the file name of the interface files must be the same as the interface name.
Implementing interfaces
After the interface is defined, the interface can be implemented in the class. Implementing an interface in a class can use the keyword implements, which has the following basic format:
[Modifier] Class < class name > [extends parent class name] [Implements interface list]{
}
modifier: optional parameter that specifies the access rights for the class, with the optional values public, abstract, and final.
class Name: required parameter, which specifies the name of the class, and the class name must be a valid Java identifier. In general, the first letter is required to capitalize.
extends parent class name : Optional parameter that specifies which parent class the class to define inherits from. When you use the extends keyword, the parent class name is a required parameter.
implements interface list: An optional parameter that specifies which interfaces the class implements. When using the Implements keyword, the interface list is a required parameter. 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 arguments and the type must be exactly the same as in 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 the following code:

  1. Public class Cire implements Calinterface

  2. {

  3. Public float getarea (float R)

  4. {

  5. float area=pi*r*r; //Calculate the area of the circle and assign the value to the variable areas

  6. return area; //Returns the calculated Circle area

  7. }

  8. Public float getcircumference (float R)

  9. {

  10. float circumference=2*pi*r; //Calculate the perimeter of the circle and assign a value to the variable circumference

  11. return circumference; //Returns the circumference length after calculation

  12. }

  13. Public Static void Main (string[] args)

  14. {

  15. Cire C = new Cire ();

  16. float f = c.getarea (2. 0f);

  17. System.out.println (Float.tostring (f));

  18. }

  19. }

In the inheritance of the class, only single-inheritance can be done, but when implementing the interface, it is possible to implement multiple interfaces at a time, separating each interface with a comma ",". In this case, there may be a constant or method name conflict, when resolving the problem, if the constant conflict, you need to explicitly specify the interface of the constant, which can be achieved through the "interface name. Constant". If a method conflict occurs, only one method can be implemented. Here is a concrete example of how to solve the above problems in detail.


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1553101

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.