I. Abstract Class
In Java, the keyword for abstraction is `abstract`. Abstract classes are created specifically for inheritance, clearly and simply telling the user and compiler what a class should look like. For example, the syntax for declaring an abstract class is:
abstract class Abc { abstract void fun(); } Abstract classes have the following characteristics:
- 1. Abstract methods must be public or protected (if they are private, they cannot be inherited by subclasses, and subclasses cannot implement the method, so it is meaningless). The default is public.
- 2. Abstract classes cannot be used to create objects directly; they must be inherited by subclasses and their parent class methods must be implemented before objects can be created.
- 3. Abstract classes can inherit from other abstract classes, and subclasses must copy and inherit the abstract methods of the parent class;
- 4. Any abstract class that contains at least one abstract method must have that method defined as abstract, regardless of whether it contains any other methods.
II. Interface
In Java, the keyword for abstraction is interface. An interface can also be considered a more abstract abstract class. It abstracts behavior, providing only a form and not the details of implementation.
The syntax of the interface is as follows:
The interface has the following characteristics:
- 1. Interfaces can contain variables, and member variables are implicitly specified as public static final variables (and can only be public static final variables; using private variables will result in a compilation error).
- 2. Interfaces can contain methods, which are implicitly specified as public abstract methods and can only be public abstract methods (using other keywords, such as private, protected, static, final, etc., will result in a compilation error). Furthermore, all methods in an interface cannot have concrete implementations; that is, all methods in an interface must be abstract methods.
- 3. A class can inherit multiple interfaces at the same time, and it needs to implement all the methods of the inherited interfaces.
III. Differences between Abstract Classes and Interfaces
After reading many articles, the main points are basically as follows:
- 1. Abstract classes can provide implementation details for member methods, while interfaces can only contain public abstract methods;
- 2. Member variables in an abstract class can be of various types, while member variables in an interface can only be of type public static final;
- 3. Interfaces cannot contain static code blocks or static methods, while abstract classes can contain static code blocks and static methods;
- 4. A class can only inherit from one abstract class, but a class can implement multiple interfaces.
So when should we use abstract classes? And when should we use interfaces?
From a practical perspective:
If we map programming to everyday life, then an abstract class represents what something is, and an interface represents what it can do. For example, a Person has eyes and skin color; these characteristics describing a person can be defined in an abstract class, while a person's behavior, such as playing basketball, can be defined in an interface.
//Abstract class Person abstract class Person { abstract void eyes(); abstract void skin(); } //Interface Action public interface Action { void playBasketball(); } So, if there's a Chinese person who can't play basketball, this class can be written like this:
public class Chinese extends Person { @Override void eyes() { System.out.print('My eyes are black'); } @Override void skin() { System.out.print('My skin is yellow'); } } There's a Russian who plays basketball; this class can be written like this:
public class Russian extends Person implements Action{ @Override void eyes() { System.out.print('My eyes are black'); } @Override void skin() { System.out.print('My skin is white'); } @Override public void playBasketball() { System.out.print('I can dunk'); } } From a programming perspective:
- 1. Abstract classes are suitable for defining the inherent attributes of a certain domain, that is, the essence, while interfaces are suitable for defining the extended functionality of a certain domain.
- 2. When it is necessary to provide common implementation code for some classes, abstract classes should be considered first. This is because non-abstract methods in abstract classes can be inherited by subclasses, making the code for implementing functionality simpler.
- 3. When code extensibility and maintainability are paramount, interfaces should be prioritized. ① There can be no hierarchical relationship between an interface and the classes that implement it. Interfaces can implement the same behavior in completely unrelated classes, making them more convenient and flexible than abstract classes. ② Interfaces only concern themselves with the methods of interaction between objects, not the specific classes to which those objects belong. An interface is a protocol between programs, making it safer and clearer than abstract classes. Interfaces are generally used more frequently.
Author: Sobin
Link: https://www.jianshu.com/p/28e3b4d61945
This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:
Original author:Jake Tao,source:"Usage and distinction between abstract classes and interfaces in Java"