The Java Learning interface concept

Source: Internet
Author: User

The Java language supports single-inheritance only, and multi-inheritance is not supported, that is, a class can have only one parent class. However, in practical applications, it is often necessary to use multiple inheritance to solve the problem. To solve this problem, the Java language provides an interface to implement multiple inheritance problems for a class.

1. Definition of the interface

Use interface to define an interface. The interface definition is similar to the definition of a class and is also the Declaration and interface body of an interface, in which the interface body consists of a variable definition and a method definition. The basic syntax format for defining an interface is as follows:

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

[Public] [Static] [Final] variable;

[Public] [Abstract] method;

 }                   &NBSP ;                          ,         &NB Sp                                

Parameters Description
Modifier An optional parameter that specifies the access permissions for the interface, and the optional value is public. If omitted, the default access permission is used
Interface Required parameter, which specifies the name of the interface, the interface name must be a valid Java identifier, in general, the first letter is capitalized
Extends list of parent interface names An optional parameter that specifies which interface the interface to be defined inherits from. The parent interface name is a required parameter when using the extends keyword
Method The methods in the interface are only defined and not implemented

Defines an interface for computation, in which a constant pi and two methods are defined.

Public interface Calculateinface {
public static final Double pi = 3.14159;//defines the constant pi used to denote pi

Public double getcircumference (double R);//define a method for calculating the perimeter

Public double Getarea (double R);//define a method for calculating the area
}

Note : As with Java class files, the file name of the interface files must be the same as the interface name.

2. Implementation of the interface

After the interface is defined, the interface can be implemented in the class. Implementing an interface in a class can use the Implements keyword, with the basic syntax in the following form:

 [Modifier] Class < class name > [extends parent class name] implements interface list {

}

Parameter descriptions that implement the syntax format of the interface:

Parameters Description
Modifier Optional parameters that specify access permissions for the class, with optional values of public, abstract, and final
Class name Required parameter, which specifies the name of the class, the class name must be a valid Java identifier, and in general, the first letter is capitalized
Extends list of parent class names An optional parameter that specifies which parent class the class to define inherits from. When using the extends keyword, the parent class name is a required parameter
Implements interface list When multiple interface names exist in the interface list, each interface is separated by commas

When implementing an interface in a class, the method name, return value type, number of arguments, and type must be exactly the same as in the interface, and all methods in the interface must be implemented.

Write a circle class that implements the interface Calculateinface defined above.

public class Circle implements calculateinface{

Public double getcircumference (double R) {
Double circumference = 2*pi*r; Calculates the perimeter of a circle and returns
return circumference;
}

Public double Getarea (double R) {//calculates the area of the circle and returns
Double area = pi*r*r;
return area;
}
}

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, using a comma "," between each interface, which is the case where a variable or method conflict can occur. When resolving the problem, if the variable conflicts, you need to explicitly specify the interface of the variable, which can be achieved through the interface name. variable. If a method conflict occurs, only one method can be implemented.

The Java Learning interface concept

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.