Deep analysis of the application of interface in Java programming _java

Source: Internet
Author: User
Tags constant definition modifier multiple inheritance in c

The essence of an interface--an interface is a special kind of abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods.

Abstract classes have something that interfaces can have, if all the methods in an abstract class are abstract, there is no method that requires this abstract class to be implemented, and all the variables in this abstract class are static variables that cannot be changed (final). Such an abstract class can then be defined as an interface (interface). The format for defining a class as an interface is to replace the keyword class of the declaring class with the keyword interface of the declarative interface.

  An interface (interface) is a special abstract class in which all methods are abstract and the attributes of this abstract class (that is, member variables) are declared as "public static final Type property names" and are declared as "public" by default. Static final "that is, the members of the variables are public, static, can not be changed." Therefore, when declaring constants in an interface, it can be written as "public static final type constant name =value (value)" or as "type constant name =value (value)" such as: "public static final int id=10" Can be written directly as "int id=10", because in the interface the default property declaration is "public static final", so "public static final" can be omitted not to write. The abstract method declared inside the interface can be identified without writing the abstract keyword, because all the methods in the interface are abstract, so the "abstract" keyword is omitted by default, as in an interface that declares three methods: "public void Start" , public void Run (), and public void stop () are not previously identified with the abstract keyword, but they are abstract methods, because the method of declaring in an interface is an abstract method, So the abstract method inside the interface omits the abstract keyword, because the method of the default declaration is abstract, there is no need to write "abstract" again, which is different from declaring an abstract method inside an abstract class, in which an abstract method is declared to use " Abstract "keyword, while in the interface to declare an abstraction method can omit" abstract ". Note: The abstract method declared inside the interface defaults to "public", and can only be "public" to make this statement is to correct the multiple inheritance in C + + where the problem prone to problems, multiple inheritance C + + is prone to problems, The problem is that if they have the same member variables between multiple inherited parent classes, this reference can be quite cumbersome and can cause a variety of problems when running. Java to fix this problem, all the member variables in the interface are changed to static final, the member variable is a static type, then this member variable belongs to the entire class, rather than belonging to an object. For multiple inheritance, there are actually more than one parent class object inside a Subclass object, and for single inheritance, there is only one parent class object inside the subclass object. Multiple inheritance subclass objects have more than one parent class object, and there may be duplicate member variables between these parent objects, which is very easy to do, so the problem is avoided in Java, and the interface is used to implement multiple inheritance. As an interface, a class can inherit from the interface (or interface), this is also more inheritance, the interface of the member variables are not specifically belong to an object, are static member variables, belong to the entire class, so a class to implement a number of interfaces is also indifferent, there will be no conflict between the objects of the problem. Implementing multiple interfaces also enables multiple inheritance, and avoids problems with multiple inheritance, which is the benefit of implementing multiple inheritance with interfaces.

1, define an interface
    use interface to define an interface. Interfaces define similar definitions, and are also divided into interface declarations and interface bodies, where the interface body consists of two parts: constant definition and method definition. 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 rights 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 legitimate Java identifier. Under normal circumstances, the initial letter is required.
Extends: List of parent interface names: An optional parameter that specifies which parent interface the interface to define inherits from. When you use the extends critical
              Word, the parent interface name is a required parameter.
Method: The methods in an interface are defined but not implemented.
For example, define an interface for calculation, in which a constant pi and two methods are defined, with the following code:

 public interface Calinterface {Final float pi=3.14159f;//defines the constant PI float g used to represent PI 
Etarea (float R);//define a method for calculating the area getarea () float getcircumference (float R);//define a method for calculating the perimeter getcircumference ()} 

Note:
    As with Java class files, interface files must have the same file name as the interface name.
2, after the implementation interface
interface is defined, you can implement the interface in the class. Implementing an interface in a class can use the keyword implements, and its basic format is as follows:
        [Modifier] class < class name > [Extends parent class name ] [Implements interface list]{
           ... .......
       }
modifier: An optional parameter that specifies the access rights of the class, and the optional value is public, abstract, and final.
Class Name: A required parameter that specifies the name of the class, and the class name must be a legitimate Java identifier. Under normal circumstances, the initial letter is required. The
extends parent class name: An optional parameter that specifies which parent class the class to define inherits from. When you use the extends keyword, the parent class name is required to
Select the parameter.
Implements interface list: An optional parameter that specifies which interfaces the class implements. The list of interfaces is required when using the Implements keyword. When multiple interface names exist in the interface list, separate the interface names with commas.
    When implementing an interface in a class, the method's name, return value type, number of parameters, and type must be exactly the same as the interface, and all the methods in the interface must be implemented. For example, write a class called Cire that implements the interface calculate defined in section 5.7.1, as follows:

public class Cire implements Calinterface  
{public 
  float Getarea (float r)  
  { 
    float area=pi*r*r;// Calculate the area of the circle and assign it to the variable areas 
    return area;//returns the calculated Circle area 
  } public 
  float getcircumference (float r)  
  { 
    float Circumference=2*pi*r;   Compute circle Circumference and assign value to variable circumference return 
    circumference;      Returns the calculated 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 inheritance of the class, only single inheritance can be done, and when implementing an interface, multiple interfaces may be implemented at once, separated by commas "," between each interface.
In this case, a constant or method name conflict may occur (in multiple interfaces), and when the problem is resolved, if the constant conflicts, you need to explicitly specify the interface for the constant, which can be implemented through the interface name. Constant. If a method conflict occurs, it is only possible to implement one method.

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.