JAVA interfaces and abstract classes, JAVA interface abstract classes
In the concept of object-oriented, we know that all objects are depicted through classes, but not all classes are used to depict objects, if a class does not contain enough information to depict a specific object, such a class is an abstract class.
Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains. They are abstractions of a series of seemingly different but essentially identical specific concepts, we cannot instantiate them (we can't get a specific thing) So it is called abstraction.
For example, we want to describe "Fruit", which is an abstraction. It has some commonalities such as quality and volume (fruit has quality), but lacks features (Apple and orange are both fruits, they have their own characteristics). We can't get the only thing that can represent the fruit (because Apple and orange cannot represent the fruit). It can be described using abstract classes, therefore, abstract classes cannot be instantiated. When we use a class to describe "apple", this class can inherit the abstract class describing "Fruit". We all know that "apple" is a kind of "Fruit ".
In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions, but this group of actions can have any specific implementation method. This abstract description is an abstract class, and any specific implementation of this group may be represented by all the derived classes of this abstract class.
All abstract methods in interfaces and abstract classes cannot be implemented in detail. Instead, all abstract methods should be implemented in their subclasses (function bodies should be available, even if {} is empty ), java designers may consider the flexibility of abstract methods. each subclass can implement abstract methods as needed.
Abstract class is defined as follows:
Public abstract class AbstractClass // there must be at least one abstract method {public int t; // common data member public abstract void method1 (); // abstract method, the subclass of an abstract class must implement the abstract method public abstract void method2 (); public void method3 (); // non-abstract method public int method4 (); publi int method4 (){...... // The default behavior that can be granted to non-abstract method methods in the abstract class, that is, the specific implementation of the method} public void method3 (){...... // The default behavior that can be granted to non-abstract methods in the abstract class, that is, the specific implementation of the method }}
The interface is defined as follows:
Public interface Interface {static final int I; // The interface cannot contain common data members. It can only have static data members that cannot be modified. static indicates global, and final indicates unchangeable, static final modification is not required and is implicitly declared as static and final public void method1 (); // methods in the interface must be abstract methods, so public void method2 () is not required (); // The interface cannot assign the default behavior of the method, that is, there cannot be a specific implementation of the method}
In short, an abstract class is a class with incomplete functions. An interface is only a set of abstract method statements and static data that cannot be modified. Neither of them can be instantiated.
In a sense, an interface is a special form of abstract class. in java, an abstract class represents an inheritance relationship. A class can only inherit one abstract class, A class can implement multiple interfaces. In many cases, the interface can indeed replace the abstract class, if you do not need to deliberately express the inheritance of attributes.
For further understanding, for the purpose of introducing abstract classes and interfaces in Java, the following answers are obtained from the experts:
1. From the perspective of the hierarchy of classes, abstract classes are at the top of the hierarchy. However, in actual design, abstract classes should be later. Why? In fact, retrieving abstract classes is a bit like extracting the formula in mathematics: ax + bx. x is an abstract class. If you don't have the formula above, how do you know if x is a formula? At this point, it is also in line with the process of people's understanding of the world, first concrete and then abstract. Therefore, if you obtain a large number of specific concepts and find their commonalities in the design process, it is correct that the set of commonalities is an abstract class.
2. The interface is similar to the abstract class on the surface, but its usage is completely different. Its basic function is to combine irrelevant classes (concepts) to form a new "New Class" that can be operated centrally ". A typical example for students is "driver ". Who can be a driver? Anyone can get a driver's license. So whether you are a student, white-collar, blue-collar, or boss, if you have a driver's license, you will be the 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, the Car object does not care about what the driver is doing. The only thing they have in common is that they have obtained a driver's license (all implemented the DriverLicence interface ). This should be the most powerful part of the interface and incomparable to abstract classes.
Abstract: abstract classes extract the common expression of a specific class, and interfaces are used to aggregate irrelevant classes into a common group. Generally, we usually develop a good habit of using multiple interfaces. After all, java is a single inheritance, unlike C ++, however, you must use an abstract class (similar to goto.