This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

An abstract class

Abstract classes are not allowed to be instantiated, only inherited, so we cannot create an instance of an abstract class through new.

Abstract classes can contain properties and methods. Methods must have abstract methods. Only classes that contain abstract methods are called abstract classes.

Subclasses inherit from abstract classes and must implement all of the abstract methods in the abstract class.

interface

Abstract classes themselves are also classes, but more abstract classes, and interfaces are even more abstract than abstract classes, and many places will say that the difference between an abstract class and an interface is the difference between IS-A and HAS-A, so you can feel for yourself.

Interfaces can be understood as a protocol, a standard, I am only responsible for definition, not implementation, but in Java 8 interface can also define the default method.

The Repository layer defines a set of interfaces, while the Service layer does not need to know the implementation. It does not matter whether the underlying database is MySQL, Oracle, or even Redis.

Use of abstract classes and interfaces

Abstract class need to pay attention to when using, because all the subclasses need to implement all its abstract method, so at the time of definition, make sure the relationship between parent-child classes, and methods of an abstract class not too much, and the definition of the interface, is to define a standard, may be the standard of data interaction, a subclass to implement interface on behalf of the subclass have some kind of ability.

Multiple inheritance is not supported in Java, but it is possible to achieve similar functionality with multiple implementations. Abstract classes are used for template methods to abstract parts of a template, whereas interfaces are completely abstract.

Application of abstract classes – template design patterns

A template design pattern is a set set of patterns. If you have the following requirements, calculate the runtime of the code. Routine is to record the start time, operation method, record the end time, the last reduction to get the result. You can do it like this.

public abstract class SupClass { public abstract void run(); // This is a template, limited to final to prevent subclasses from overwriting the method. public final void calculateTime(){ long l1 = System.currentTimeMillis(); run(); long l2 = System.currentTimeMillis(); long time = l2 - l1; System.out.println(" run time is "+ time +" milliseconds. ); }} public class extends SupClass{@override public void run() {system.out.println (" calculate the run of subclass Method execution time..." ); long sum = 0; for (int i = 0; i < 1000000000; i++) { sum = sum + i; }} public class extends SupClass{@override public void run() {system.out.println (" calculate the run of subclass Method execution time..." ); long sum = 0; for (int i = 0; i < 10000000; i++) { sum = sum + i; Public class TemplateTest {public static void main(String[] args) {SupClass class1 = new Sub1Class(); class1.calculateTime(); SupClass class2 = new Sub2Class(); class2.calculateTime(); }}Copy the code

Application of the interface – Factory mode

In factory mode, we create objects without exposing the creation logic to the client, and by using a common interface (Car) to point to the newly created object. That’s one way to create a class. This is mainly reflected in the internal logic of the methods used to get objects in the factory. Like the getCar method in the following example.

public interface Car { void run(); } public implements Car{@override public void run() {system.out.println (" implements Car. ); }} public implements Car{@override public void run() {system.out.println (" implements Car ") {@override public void run() {system.out. ); }} public implements Car {@override public void run() {system.out.println (" implements Car ") {@override public void run() {system.out.println (" ); }} /** * Factories decide how objects are created and why they are called factories, i.e. how objects are created is decided here. * */ public class CarFactory {// return the same interface. This is also an upward transformation of polymorphism. public Car getCar(String type){ if(type == null){ return null; } if(type.equalsIgnoreCase("SMALLCAR")){ return new SmallCar(); } else if(type.equalsIgnoreCase("BIGCAR")){ return new BigCar(); } else if(type.equalsIgnoreCase("BAOMA")){ return new BaoMa(); } return null; Public class FactoryTest {public static void main(String[] args) {CarFactory factory = new CarFactory(); Car car = factory.getCar("smallcar"); car.run(); Car car2 = factory.getCar("bigcar"); car2.run(); Car car3 = factory.getCar("baoma"); car3.run(); }} I am SmallCar, look exquisite! I am BigCar, I am not afraid of collisions! I laugh in BaoMa.Copy the code

conclusion