The difference between an interface and an abstract class

Source: Internet
Author: User

Just ended J2SE's study, in the J2SE, mentioned the Java interface and the abstract class, they function similarly, then they have what difference? How to apply it? I query a lot of information, sorting knowledge as follows, if there are errors, please correct me.


In the Java language, abstract classes and interface are the two mechanisms that support the definition of an abstraction class. It is the existence of these two mechanisms that gives Java a powerful object-oriented capability. Abstract class and interface are very similar in terms of support for the definition of abstractions, and can even be replaced, so many developers are more likely to choose abstract classes and interface when they are doing the abstraction class definitions. In fact, there is a great difference between the two, for their choice even reflects the nature of the problem areas of understanding, the design intent of the understanding is correct, reasonable. This article will analyze the differences between them and try to provide a basis for developers to choose between them.



   Understanding Abstract Classes

Abstract class and interface are used in the Java language for abstraction classes (the abstract class in this article is not translated from abstract class, it represents an abstract body, while abstract Class is a method of defining abstract classes in the Java language, which the reader is aware of, and what are abstract classes, and what are the benefits of using abstract classes?

In object-oriented concepts, we know that all objects are depicted through classes, but not in the opposite way. Not all classes are used to depict objects, and 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 characterize the abstract concepts we derive from analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same. For example, if we develop a graphical editing software, we will find that there are some specific concepts of circle and triangle in the problem domain, they are different, but they all belong to the concept of shape, the concept of shape does not exist in the problem domain, it is an abstract concept. Abstract classes used to characterize abstract concepts cannot be instantiated because abstract concepts have no corresponding specific concepts in the problem domain.


Abstract classes are primarily used for type concealment in the object-oriented realm. We can construct an abstract description of a fixed set of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any possible concrete implementation of this group is represented by all possible derived classes. The module can manipulate an abstract body. Because the module relies on a fixed abstraction, it can be modified, and the behavior of this module can be extended by deriving from this abstraction. Readers familiar with OCP must know that abstract classes are key in order to achieve a core principle OCP (open-closed Principle) for object-oriented design.


from the syntactic definition level to the abstract class and interface at the syntactic level, the Java language gives different definitions for abstract class and interface, and the following defines an abstract class called demo as an example to illustrate this difference.


The way to define the demo abstract class using the abstract class is as follows:

<span style= "FONT-SIZE:18PX;" >abstract class demo{abstract void method1 (); abstract void method2 (); }</span>


The way to define the demo abstract class using interface is as follows:

<span style= "FONT-SIZE:18PX;" >interface demo{void method1 (); void Method2 (); ...} </span>

In the abstract class mode, the demo can have its own data members, but also can have non-abstract member methods, and in the implementation of the interface method, the demo can only have static data members can not be modified (that is, must be static final , but the data members are not generally defined in interface, all the member methods are abstract. In a sense, interface is a special form of abstract class.

from a programmatic point of view

Both the abstract class and the interface can be used to implement the idea of "design by contract". However, there are some differences in the specific use.


First, the abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship (because Java does not support multiple inheritance----). However, a class can implement multiple interface. Perhaps this is a compromise of the Java language designer in considering Java's support for multiple inheritance.

Second, in the definition of abstract class, we can give the method the default behavior. However, in the definition of interface, the method does not have the default behavior, and in order to circumvent this restriction, the delegate must be used, but this adds some complexity and can sometimes cause a lot of trouble.

in the There is another serious problem with the inability to define default behavior in an abstract class, which can cause maintenance headaches. Because if you later want to modify the interface of the class (typically represented by an abstract class or interface) to accommodate the new situation (for example, adding new methods or adding new parameters to a used method), it can be very cumbersome and may take a lot of time (for many of the derived classes , in particular). However, if the interface is implemented by an abstract class, it is possible to modify the default behavior defined in the abstract class only. Similarly, if the default behavior cannot be defined in an abstract class, it causes the same method implementation to appear in each of the derived classes of the abstract class, violating the "one Rule,one place" principle, resulting in code duplication, which is also detrimental to future maintenance. Therefore, you should be very careful when choosing between the abstract class and the interface.



   Abstract class and interface from the perspective of design concept


It mainly discusses the difference between abstract class and interface from the angle of grammar definition and programming, and the difference between these layers is comparatively low-level and non-essential. This section examines the difference between the two, from another level: the design concepts reflected by the abstract class and interface. The author thinks that the analysis from this level can understand the essence of the two concepts.


As mentioned earlier, abstract class embodies an inheritance relationship in the Java language, in order to make the inheritance relationship reasonable, there must be a "is-a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be the same in nature. For interface, it does not require that the interface and interface definitions be consistent in the concept, but only the contract that implements the interface definition.

  Summary


1.abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface.

2. In abstract class, you can have your own data members, or you can have non-ABSTARCT member methods, and in interface, you can only have static data members that cannot be modified (that is, it must be static final, but Data members are not generally defined in interface, and all member methods are abstract.

the 3.abstract class and interface reflect a different design concept. In fact, abstract class represents the "is-a" relationship, interface represents the "like-a" relationship.

4. Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

5. The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed.

6. Variables in abstract classes are friendly by default, whose values can be redefined in subclasses or re-assigned.

7. The methods in the interface are public,abstract type by default.


Abstract class and interface are two of the ways in which abstractions are defined in the Java language, and there is a great similarity between them. However, their choices often reflect an understanding of the nature of the concepts in the problem domain, the correctness and reasonableness of the design intent, as they represent the different relationships between concepts (though they are capable of fulfilling the function of demand).

The difference between an interface and an abstract class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.