Interface with implementation of the interface and the interface callbacks

1. interfaces
we use the keyword interface to declare an interface. And Class Definition is similar interface, interfaces and interface declarations into the body. E.g:

interface Printable{
  final int MAX = 100;
  void add();
  float sum(float x,float y);
}

The interface body only abstract methods and constants in a statement.

2. implement interface
(1) to implement the interface class in the abstract class interface to override by, both abstract class can override the methods in an interface, the method may have a direct interface.

(2) implementation of the interface class must have all interface methods.

(3) a class can implement multiple interfaces, for example:

class A implements Printable,Addable{
....
}

(4) If the parent class implements an interface, it is naturally subclass implements the interface.

(5) the interface can also be inherited, that may be an interface sub-interface extends another interface keyword statement. Because interface methods and constants are public, sub-interface inherits all the methods and variables parent interface.

(6) the interface is the callback interface declaration refers to the use of a variable can be called a class that implements the interface methods, and the transformation of similar objects.

3. interfaces and polymorphism
generated by the interface Polymorphism refers to different classes in the same time achieve an interface may have different implementations, the interface variables may have multiple forms at the interface a callback method.

4. Interface Callback
Interface callback interface declaration refers to a variable, you can call a class interface methods implemented.
example:

interface showMessage{
  void showSome(String s);
 }
  class TV implements ShowMessage{
    public void showSome(String s)
    {
       System.out.println(s);
     }
 }
 public PC implements ShowMessage{
   public void showSome(String s)
   {
      System.out.println(s);
   }
 }
 public class Example{
 public static void main(String args[]){
  ShowMessage sm;
  sm = new TV();
  sm.showSome("长虹电视机");//接口回调
  sm = new PC();
  sm.showSome("华为电脑");//接口回调
  }
  }
Published 35 original articles · won praise 0 · Views 1300

Guess you like

Origin blog.csdn.net/c1776167012/article/details/102876671