Explain the difference between interface and abstract class in Java
Explain the difference between interface and abstract class in Java
1. Abstract class represents an inheritance relationship in the Java language. A class can only use the inheritance relationship once. However, a class can implement multiple interfaces.
2. In the abstract class, you can have your own data members or non Abstract member methods. In the interface, you can only have static data members that cannot be modified (that is, they must be static final, but data members are generally not defined in the interface). All member methods are abstract.
3. Abstract class and interface reflect different design concepts. In fact, abstract class represents the "is-a" relationship, and interface represents the "like-a" relationship.
4. Classes that implement abstract classes and interfaces must implement all of their methods. Abstract classes can have non abstract methods. There can be no implementation method in the interface.
5. The variables defined in the interface are public static final by default, and their initial values must be given. Therefore, the implementation class cannot redefine or change their values.
6. Variables in abstract classes are friendly by default, and their values can be redefined or re assigned in subclasses.
7. All methods in the interface are public and abstract by default.
Conclusion
Abstract class and interface are two ways to define abstract classes in the Java language, and they have great similarities. However, their choice often reflects the understanding of the essence of concepts in the problem field and whether the reflection of design intention is correct and reasonable, because they show different relationships between concepts (although they can realize the required functions). In fact, this is also a common usage of language. I hope readers can understand it carefully.
Main differences:
1. Single inheritance and multiple inheritance
2. Abstract classes can have their own implementation methods and non abstract methods. Data variables can not be public. The methods of the interface are public. They do not have their own benefit domain and cannot implement their own methods. All methods are abstract and can only have static data.
3. Design intention, one is an abstraction, the other is to realize what I can do.
If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!