Java programming ideas - detail Java interface

Interfaces give us a more structured way of separating the interface from the implementation. There are usually two purposes in choosing to use an interface:

  • First, in order to be able to transform upwards
  • The second is to avoid creating objects (instances), which is consistent with abstract classes.

Any abstraction is generated in response to real needs, so interfaces cannot be used blindly in development, but according to design requirements.

The advice given in "Java Programming Ideas" is to choose classes first, and if the necessity of the interface becomes very clear, then refactor .

1. Abstract classes and abstract methods

Abstract classes provide a set of generic interfaces through which it is hoped that a series of classes can be manipulated

Abstract method - an incomplete method, with only a declaration and no method body, as follows

abstract void f();//Contains the keyword abstract

Abstract class - a class that contains one or more abstract methods and is modified with the keyword abstract

//create abstract class
abstract Animal{
   public void eat(String time);//Abstract method
   public void sleep(){
      System.out.print("Sheep!");
   }
}

class Dog extends Animal{
   public void eat(String time){    
     System.out.print("Dog "+time+"eat meat!");
   }
}
class Sheep extends Animal{
   public void eat(String time){
    System.out.print("Sheep "+time+"eat grass");
   }
}
public class Test{
   public void turn(Animal a){
      a.eat("8:00");
      a.sleep();
   }
   public static void main(String[] args){
     Animal[] a={new Dog(),new  Sheep()};
     for(Animal sub:a){
       turn(sub);
     }
    }
}
out:
Dog 8:00 eat meat!
Sleep!
Sheep 8:00 eat grass!
Sleep!

It should be noted that abstract classes cannot be instantiated , so Java uses the " abstract method mechanism " in order to make it clear to users , and uses its incompleteness and the use of the keyword abstract to inform the compilation The particularity of the device itself to prevent misleading users.

2. Interface

       An interface is an extremely abstract class . The methods in the interface also have no method body. It exists to maintain the consistency of some other classes . To create an interface , you need to use the keyword interface instead of class . You can add a qualification modifier before the keyword, but like a class, there can only be one interface modified by public in an interface file , and the interface name must be the same as the file name. Interfaces can also contain fields, but these fields are implicitly static and final .

The use form of interface is very similar to inheritance, but it is very different from inheritance. Inheritance requires that each subclass can only inherit one parent class, but the interface can implement multiple interfaces through the implements keyword (equivalent to creating a types that can be upcast to multiple base classes, much like multiple inheritance),

It should be noted that all methods in an interface should be declared public, because the Java compiler does not allow the access rights of the interface to be reduced.

Specific examples of interfaces are as follows:

package com.interface;
//create interface
public interface Animal{
   String time="8:00";//static&final
   public void eat(String time);
   public void sleep();
}
package com.animalclass;
import com.interface.Animal;//Import interface

class Dog implements Animal{
   public void eat(String time){    
     System.out.print("Dog "+time+"eat
 meat!");
   }
   public void sleep(){
     System.out.print("Dog sleep!");
   }
}
class Sheep implements Animal{
   public void eat(String time){
    System.out.print("Sheep "+time+"eat
 grass");
   }
   public void sleep(){
     System.out.print("Sheep sleep");
   }
}
public class Test{
   public void turn(Animal a){
      a.eat(Animal.time);//time is static by default, so it can be called directly through the interface name
      a.sleep();
   }
 public static void main(String[] args){
     Animal[] a={new Dog(),new  Sheep()};
     for(Animal sub:a){
       turn(sub);
     }
    }
}
out:
Dog 8:00 eat meat!
Dog sleep!
Sheep 8:00 eat grass!
Sheep sleep!

Third, the domain in the interface

  • The defaults are static and final types , and are automatically public;
  • Can't be a variable, can't be "empty final", but can be a non-const expression;
Random RAND=new Random(20);
int ONE=RAND.nextInt(10);
double TWO=RAND.nextDouble()*10;
//Note: These fields are not part of this interface, they are stored in the static storage area of ​​the interface and can be called directly by the interface name

  • Interface fields complemented when Java didn't enumerate enums, but fields are rarely created in interfaces these days.

Fourth, the nested interface

That is, interfaces can be nested with each other. This nesting is mainly divided into two types:

  • nested interface in class
class A{
   public interface B{void show();}
   public interface C{void show();}
   public interface D{void show();}
   //The nested interface can also be a private interface, but the interface at this time cannot be accessed by external classes or interfaces, only through methods (similar to accessing private member variables in a class requires a get method)
   private interface E{void show();}
}

  • interface nested interface
interface A{
   public interface B{void show();}
   public interface C{void show();}
   public interface D{void show();}
}

In addition to the above, the book also introduces the problem of complete decoupling of interfaces and the application of interfaces in design patterns (strategy design pattern, factory design pattern). These two points are very abstract, so I will focus on learning and Record.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325945011&siteId=291194637
Recommended