java interface concept and use

Package java11;
 / * 
in any java version, the interface can abstract methods defined 
format: 
public abstrace method return type name (parameter list); 

Note: 
1, among interface abstract methods, modifier must be two fixed keywords, public abstract 
2, these two keywords modifiers, may be selectively omitted 
three elements 3, the method may be defined arbitrarily 
* 
* * / 
public  interface MyinterfaceAbstrace {
     // this is an abstract method 
    public  abstract   void methodAbs1 ( ); 

    // this is an abstract method 
    abstract   void   methodAbs2 (); 

    // this is an abstract method 
    public   void   methodAbs3 (); 

    // this is an abstract method 
    void   methodAbs4 (); 

} 


Packagejava11; 

public  class MyinterfaceAbstractImpl the implements MyinterfaceAbstrace { 
    @Override 
    public  void methodAbs1 () { 
        System.out.println ( "This is the first method" ); 
    } 

    @Override 
    public  void methodAbs2 () { 
        System.out.println ( "This is The first method " ); 

    } 

    @Override 
    public  void methodAbs3 () { 
        System.out.println ( " this is the first method " ); 

    } 

    @Override 
    public  void methodAbs4 () { 
        System.out.println ("This is the first method" ); 

    } 
} 


Package java11;
 / * / 
Interface Specification plurality of classes is a common 
interface is a reference data types, the most important elements is the abstract methods 

defined format: 
public interface interface name { 
    / / interface content 
} 
Although keywords into the interface, but the compiled byte code file is still .class 
content contained in the interface: 
    constants, abstract method, the default method, static methods, private methods 

steps used by the interface: 
1, interface can not be directly used, there must be a "implementation class" to "realize" the Interface 
format: 
public class implements the interface implementation class name {name 
// ..... 
} 
2, the interface implementation class must overwrite (implemented) All abstract interface methods 
to achieve: remove the abstract keyword, family Hassan method body braces 
3, create an object implementation class, conducted using 

Note: 
If the class does not implement all of the abstract methods interface overwritten, then this implementation class and that they must be abstract class 
* 
* * / 
public  class{DemoInterface
     public  static  void main (String [] args) {
 //         MyinterfaceAbstrace MyinterfaceAbstrace new new Inter = (); error writing 

        // create objects using implementation class 
        MyinterfaceAbstractImpl impl = new new MyinterfaceAbstractImpl (); 
        impl.methodAbs1 (); 
        impl.methodAbs2 (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/spp666/p/11731492.html