An abstract class

The use of the abstract keyword

Abstract: abstract

2. Abstract can be used to modify the structure: class, method

  1. Abstract modifiers: Abstract classes

This class cannot be instantiated

There must be a constructor in the abstract class, which is easy to call when subclass instantiation (involving: subclass object instantiation process)

In development, subclasses of abstract classes are provided for instantiation of subclass objects to complete related operations

  1. Abstract: Abstract method

Abstract methods have only method declarations, not method bodies

A class that contains abstract methods must be an abstract class. Conversely, an abstract class can have no abstract methods.

A subclass may not be instantiated until it overrides all the abstract methods in its parent class

If a subclass does not override all the abstract methods in its parent class, it is also an abstract class and needs the abstract modifier

Note on the use of abstract:

1. Abstract cannot be used to modify: attributes, constructors, etc

2. Abstract cannot be used to modify private methods, static methods, final methods, or final classes

// An anonymous subclass of an abstract class
public class PersonTest {
  
  public static void main(String[] args) {
    
    method(new Student());// Anonymous objects
    
    Worker worker = new Worker();
    method1(worker);// Non-anonymous classes non-anonymous objects
    
    method1(new Worker());// Non-anonymous class anonymous objects
    
    System.out.println("* * * * * * * * * * * * * * * * * * * *");
    
    // Create an anonymous subclass: p
    Person p = new Person(){

      @Override
      public void eat(a) {
        System.out.println("吃东西");
      }

      @Override
      public void breath(a) {
        System.out.println("Just breathe."); }}; method1(p); System.out.println("* * * * * * * * * * * * * * * * * * * *");
    // Create an anonymous object for an anonymous subclass
    method1(new Person(){
      @Override
      public void eat(a) {
        System.out.println("Eat good food.");
      }

      @Override
      public void breath(a) {
        System.out.println("Get some fresh air."); }}); }public static void method1(Person p){
    p.eat();
    p.breath();
  }
  
  public static void method(Student s){}}class Worker extends Person{

  @Override
  public void eat(a) {}@Override
  public void breath(a) {}}Copy the code

Interface interface

/* * The use of an interface * 1. The use of an interface is defined by an interface * 2. How to define an interface: Define the members of the interface * * 3.1 JDK7 and previously: define only global constants and abstract methods * > Global constants: public static final. Public abstract * * 3.2 JDK8: In addition to defining global constants and abstract methods, we can also define static methods and default methods * * 4. Interface cannot define a constructor! In Java development, an interface is used by making a class implement (implements). * If the implementation class overwrites all the abstract methods in the interface, the implementation class can be instantiated. Class AA extends BB implements CC,DD,EE * * 7. Can interface and the interface between inheritance, but also multiple inheritance * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 8. 9. Interfaces, in fact, can be regarded as a specification * * Interview question: What are the similarities and differences between abstract classes and interfaces? * * /
Copy the code
/* * Interface application: proxy mode * */
public class NetWorkTest {
  public static void main(String[] args) {
    Server server = new Server();
// server.browse();
    ProxyServer proxyServer = newProxyServer(server); proxyServer.browse(); }}interface NetWork{
  
  public void browse(a);
  
}

// Proxy class
class Server implements NetWork{

  @Override
  public void browse(a) {
    System.out.println("Real server accessing the network"); }}/ / the proxy class
class ProxyServer implements NetWork{
  
  private NetWork work;
  
  public ProxyServer(NetWork work){
    this.work = work;
  }
  

  public void check(a){
    System.out.println("Check before networking.");
  }
  
  @Override
  public void browse(a) { check(); work.browse(); }}Copy the code