Object-oriented interface to the java

Interface concept

    Java interface is a declaration of a series of methods, is a collection of some of the features of the method, an interface features not only the method of implementation, so these methods can be implemented in different classes in different places, but these implementations can have different behavior (function).

 
The characteristics of the interface

    is like a class, an interface can have methods and properties, but the methods declared in default interface is abstract. (I.e., method identifier only, and no method body).

 

    Interface specifies what a class must do and can not do, equivalent to the class blueprint.
    An interface that describes an ability, such as "athlete" can be used as an interface, and any implementation "player" class interface must have the ability to achieve run this action (or implement move () method), so the role of the interface is to tell class, you want to realize the function of this interface represents me, you will have to implement some way I can admit that you do have some ability this interface represents.
    If a class implements all the methods required in an interface, but provides no way body but only only way to identify, then this class must be an abstract class. (It must be remembered: abstract method can only exist in the abstract class or interface, but the non-abstract class abstract method able to exist, i.e., there are methods body interface is an abstract class hundred percent.)
    An example of a library interface is JAVA : Comparator interface that represents this ability "can be compared," as long as any class that implements Comparator interface, then this class also have "more" this capability, it can be used for the sort operation.

Why use Interface

        interface is used to describe an abstraction.
    Because unlike C ++ as Java support multiple inheritance, so Java can make up for this limitation by implementing the interface.
    Interface can also be used for decoupling.
    Interface is used to implement the abstract, but can also be used to implement an abstract class abstract, and why it must use an interface? What is the difference between interfaces and abstract classes have it? The reason is that internal abstract class may contain non-final variable, but the variable is present in the interface must be the final, public, static's.

 


The syntax that implements the interface

    to declare an interface, we use the interface keyword, all the methods in the interface must be declared only way to identify, rather than going to declare a specific method body, because the body to achieve specific method inherited by this interface class come achieved, therefore, the interface is not the specific realization tube. Interface attribute default Public Static Final. A class implements the interface must implement all the abstract methods defined in this interface.

Use interface description:

java interface is a set of abstract methods using interface for programming. java programming interface is defined by a series of abstract method, then a custom implementation class, an abstract method to implement an interface, the interface can be hidden by the internal processing

  1. How to declare a java interfaces? Declare a java interface needs to be modified using the keyword interface, created using development tools that automatically create add keywords

  2. How many interfaces interfaces can inherit? A plurality of interfaces can inherit an interface, but java class can inherit from a parent class, are single inheritance patterns

  3. Interface which can define a variable, but it must be publicly accessible static const

  4. Interfaces which can define a method, a conventional method after being compiled by the compiler are abstract, default add

    abstract keyword

  5. Interfaces which can define a static method, a static method can body methods, static methods belong to the class-level methods, can be directly invoked by the class

  6. Implement interface methods need to use the implements keyword

    code show as below:

    public  interface Smoking {
     //     abstract methods public abstract method return type name (parameter list)
     //     characteristics member variable public static final variable name = variable value;
     //     static constant (once the assignment can not be changed)
     //     interface A { }
     //     inteface {B} 
        public  static  Final  int A =. 1 ;
         public  abstract  void Smoke (); 
        
    
    }
    public class Student implements Smoking {
        public void smoke() {
        System.out.println("学生抽烟");    
        }
    
    }
    public class demo01 {
    
        public static void main(String[] args) {
            Student s=new Student();
            s.smoke();
    System.out.println(s.a);
    System.out.println(Smoking.a);
        }
    
    }

    The output is:

     

     

Guess you like

Origin www.cnblogs.com/-lwl/p/10964290.html