This article describes the usage of interfaces and abstract classes in Java. Share it for your reference, as follows:
In the object-oriented concept, we know that all objects are depicted by classes, but not all classes are used to describe objects. If a class does not contain enough information to describe a concrete object, such a class is an abstract class.
Abstract classes are often used to represent abstract concepts we obtain in analyzing and designing the problem field. They are abstractions of a series of concrete concepts that seem different but are essentially the same. We cannot instantiate them (can't come up with a concrete thing), so we call them abstractions.
For example: When we want to describe "fruit", it is an abstraction. It has some common characteristics such as mass and volume (fruits have mass), but it lacks characteristics (apples and oranges are fruits, and they have their own characteristics). We cannot come up with the only thing that can represent fruits (because apples and oranges cannot represent fruits). Abstract classes can be used to describe it, so abstract classes cannot be instantiated. When we use a certain class to describe "apples", this class can inherit the abstract class that describes "fruit". We all know that "apples" are a kind of "fruit".
In the object-oriented field, abstract classes are mainly used for type hiding. We can construct a fixed abstract description of a set of behaviors, but this set of behaviors can have any possible concrete implementation methods. This abstract description is an abstract class, and this set of any possible concrete implementations manifests as all derived classes of this abstract class.
All abstract methods in interfaces and abstract classes cannot be implemented in concrete implementations, but should implement all abstract methods in their subclasses (must have a function body, even if it is empty in {}). Java designers may consider the flexibility of abstract methods, and each subclass can implement abstract methods according to their own needs.
The definition of abstract class is as follows:
public abstract class AbstractClass //There is at least one abstract method in it { public int t; //Ordinary data member public abstract void method1(); //Abstract method, subclasses of abstract class must implement abstract methods in abstract class in the class public abstract void method2(); public void method3(); //Non-abstract method public int method4(); public int method4(){ ... //Abstract class can be assigned the default behavior of non-abstract method methods, that is, the concrete implementation of the method} public void method3(){ ... //Abstract class can be assigned the default behavior of non-abstract method methods, that is, the concrete implementation of the method} }The definition of an interface is as follows:
public interface Interface Interface{ static final int i; //There can not be ordinary data members in the interface, only static data members that cannot be modified. Static means global, final means cannot be modified. It can be modified without static final modification, and it will be implicitly declared as static and final public void method1(); //The methods in the interface must be abstract methods, so public void method2() does not need abstract modification; //The default behavior of methods cannot be assigned to the interface, that is, there cannot be a specific implementation of the method}In short, abstract classes are incomplete classes, and interfaces are just collections of abstract method declarations and static data that cannot be modified, neither of which can be instantiated.
In a sense, an interface is a special form of abstract class. In the Java language, abstract classes represent an inheritance relationship. A class can only inherit one abstract class, while a class can implement multiple interfaces. In many cases, interfaces can indeed replace abstract classes, if you do not need to deliberately express inheritance on attributes.
To further understand, for the purpose of Java introducing abstract classes and interfaces, the answers I got from experts are as follows:
1. From the perspective of the class hierarchy, abstract classes are at the top of the level, but in actual design, abstract classes should generally appear later. Why? In fact, the acquisition of abstract classes is a bit like extracting common factors in mathematics: ax+bx, x is an abstract class. If you don’t have the previous formula, how do you know if x is a common factor? In this regard, it is also in line with people's process of understanding the world, first concrete and then abstract. Therefore, if you get a lot of specific concepts and find commonalities in the design process, it should be true that this collection of commonalities is an abstract class.
2. On the surface, interface is very similar to abstract classes, but its usage is completely different. Its basic function is to gather some irrelevant classes (concepts) together to form a new, centrally operable "new class". A typical example I give to students is "driver". Who can be a driver? Anyone can do it, as long as you get your driver's license. So I don’t care whether you are a student, a white-collar worker, a blue-collar worker or a boss, as long as you have a driver’s license, you are a driver.
interface DriverLicence { Licence getLicence(); }class StudentDriver extends Student implements DriverLicence { } class WhtieCollarEmployeeDriver extends WhtieCollarEmployee implements DriverLicence { } class BlueCollarEmployeeDriver extends BlueCollarEmployee implements DriverLicence { } class BossDriver extends Boss implements Driver { }When I define the "car" class, I can specify the "driver".
class Car {setDriver(DriverLicence driver);}At this time, Car's object did not care what the driver did. Their only common point was that they had obtained a driver's license (both implemented the DriverLicence interface). This should be the most powerful part of the interface and is incomparable to abstract classes.
Summarize:
Abstract classes extract common factors of concrete classes, while interfaces are to "hash" some unrelated classes into a common group. Usually, we develop a good habit of using multiple interfaces. After all, Java is single inheritance, unlike C++, but it must be used when using abstract classes (somewhat similar to goto), haha.
I hope this article will be helpful to everyone's Java programming.