Abstract and interface are both keywords in Java, which means they are important enough, and abstract classes and interfaces provide a great help in object-oriented programming. Let’s review the basics.

  • An abstract class

    • Interfaces may be the first thing you think of when building classes that don’t implement methods, but abstract classes are also an important and necessary tool for this purpose.

    • We want to use the abstract keyword to modify the class. We want to use this general class to operate on a set of class methods. If there is no concrete content, the purpose of this abstract class is only one: do not let other classes instantiate the object of this abstract class, can only instantiate its subclass object. To achieve manipulation, Java provides a mechanism for abstract methods, which are also decorated with the abstract keyword. Classes that contain abstract methods are called abstract classes

    • Abstract Class characteristics

        1. Abstract classes and abstract methods must be qualified with the abstract keyword
        1. Abstract classes do not necessarily have abstract methods. Classes with abstract methods must be abstract classes or interfaces
        1. Abstract classes cannot be instantiated, that is, they cannot be new. Abstract classes must be instantiated by subclasses. This is actually a kind of polymorphism. If an abstract class is instantiated, the instantiated object of the abstract class can call the abstract method of the abstract class, but the abstract method is not implemented and has no meaning, so the abstract class cannot be instantiated.
        1. The subclass of the abstract class is either the abstract class (Car in the instance) or overrides the abstract method in the abstract class (Jetta in the instance)
        1. A class can only inherit from one abstract class, and an abstract class can inherit from an abstract class (the SuperCar class in the instance).
    • Abstract class member features:

        1. A member can be either a constant or a variable, but abstract cannot modify a member variable. The value of a variable is not fixed and cannot be abstracted
        1. Abstract classes also have constructors, which are important because subclasses have access to their parent’s initialization data (the Jetta constructor super() calls the abstract superclass constructor in this example).
        1. Member methods can be either abstract or non-abstract. Abstract methods are usually methods that force subclasses to implement, while non-abstract methods are usually repetitive code that improves code reuse
        1. Static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static: static Abstract method subclasses are overridden), private key (private method subclasses are not accessible)
    • The instance

      /** * @author: MAO. Qitian * @date: 2018/8/11 0011 16:25 * @description: Car abstract class */ public abstract class Car {publicCar(){
          System.out.println("Constructor of abstract class called");
      }
      
      public void driver(){
          System.out.println("All cars can drive."); } public void speed(); } /** * @Author: [email protected] * @Date: 2018/8/11 0011 16:29 * @Description: Jetta */ public extends Car{publicJetta(){
          super();
       }
       
       @Override
       public void speed() {
          System.out.println("I can't get used to driving a Jetta after a Lamborghini."); } } /** * @Author: mao.qitian * @Date: 2018/8/11 0011 22:39 * @Description: Public abstract class extends Car {public abstract void expensive (); } /** * @Author: mao.qitian * @Date: 2018/8/11 0011 16:27 * @Description: */ public class extends SuperCar {@override public voidspeed() {
          System.out.println("Lamborghini breaks 100 in two seconds.");
      }
      
      @Override
      public void expensive() {}}Copy the code
  • Interface

    • Interface takes abstraction a step further. The interface keyword modifier produces a completely abstract class that allows the author to define method names, parameters, and return types, but it does not have any method body, providing only the form (rules) without any concrete implementation.

    • Interface features

        1. Interfaces use the interface keyword instead of the class modifier class,
        1. Class implementation interfaces are represented by Implement
        1. Like abstract classes, interfaces cannot be instantiated, only by the class that implements the interface
        1. An interface can be subclassed as an abstract class or a concrete class that overrides the abstract methods of the interface
    • Interface Member Features

        1. Variables defined in the interface are constants. The default modifier is public static final
        1. The interface has no constructor
        /**
         * @Author: mao.qitian
         * @Date: 2018/8/12 0012 0:22
         * @Description:
         */
        public interface A {
          public  void b();
         }
        
         class C implements A{
          public C(){ super(); Void Override public void Override public void Override public void Override public void Override public void Override public void Override public void Override public void Overrideb() {}}Copy the code
        1. Member methods in an interface can only be abstract methods. The default modifier is public Abstract
        1. All member methods and variables in the interface are public.
    • Application of interfaces

      • Strategy pattern: Define a series of algorithms, encapsulate each algorithm, and make them interchangeable. The policy pattern allows the algorithm to change independently of the client that uses it.

      • Three roles

        • Context roles: Manipulate the Context in which we formulate policies, using generic calls to policies
        • Stragety: Policy, the abstraction of an algorithm, usually an interface
        • Policy Implementation role (ConcreteStragety) : Implement an abstract policy interface and concretely implement this policy
        • The instance
        To realize the bonus payment of school teachers, teachers have attributes: number, name, total teaching workload, bonus bonus calculation method is as follows: Public interface Function {// Stragety public double {// Stragety public double Money(double x); } public class A implements Function {// public double Money(double x) {returnx*30; } public class B implements Function {public double Money(double x) {returnx*25; }} public class C implements Function {// public class C implements Function {returnx*20; }} public class myMoney {// context role Function s; double M; // Time String name; // Teacher name Stringtype; // int number; Public void S(double X,String N,int Num,String Type){this.m =X; this.name=N; this.number=Num; this.type=Type; } // Public doublegetMoney() {if(type.equals("Professor")) s=new A();
         if(type.equals("Associate professor")) s=new B();
         if(type.equals("Lecturer")) s=new C();
         return s.Money(M);
           }
         }
        
         myMoney f=new myMoney();
          	     Scanner sc=new Scanner(System.in);
          	     System.out.println("Please enter position :");
          	     String Type=sc.next();
          	     System.out.println("Please enter name :");
          	     String N=sc.next();
          	     System.out.println("Please enter the number :");
          	     int Num=sc.nextInt();
          	     System.out.println("Please enter time :"); double X=sc.nextDouble(); f.S(X, N, Num, Type); System. Out.println (N + bonus for"+f.getMoney());
        Copy the code
  • Class to class, class to interface, interface to interface

    • From class to class, a class can inherit only one class, but classes can inherit multiple layers
    • Class and interface is the implementation relationship, a class can inherit an interface, can inherit multiple interfaces, can inherit a class at the same time implement multiple interfaces
    • Interfaces are inherited. An interface can inherit from another interface or multiple interfaces
    /** * @Author: mao.qitian * @Date: 2018/8/12 0012 0:22 * @Description: */ public interface A { public void a(); } interface B { public void b(); } interface C extends B,A{public void C (); } class D implements A,B,C{ @Override public voida() { }
      @Override
      public void b() { }
      @Override
      public void c() {}}Copy the code
  • The difference between abstract classes and interfaces

class Members of the difference between Distinction of inheritance Differences in design concepts
An abstract class Member variables can be constants or variables, have constructors, and member methods can be abstract or nonabstract Single inheritance, multiple inheritance The inherited representation is the relation of “is A”, and the common function of the inherited system is defined in the abstract class
interface Member variables can only be constants, there are no constructors, and member methods can only be abstract Implementation, can realize multiple interfaces The inherited representation is the “like A” relation, and the interface defines the extended functions of the inherited representation

The last

Still that saying, a good memory is better than bad writing, through this article, once again consolidated the basic knowledge. If there is something wrong in the article, please leave a comment and point it out to me. Let’s learn and progress together.

  • References:

    • Android Advanced Light
    • Ideas for Java Programming (4th edition)
    • Synchronize abbreviated address