Out the perplexity of abstract class and interface

Source: Internet
Author: User
Tags abstract inheritance

Abstract class and interface are two mechanisms that support the definition of abstract classes in the Java language, and it is because of the existence of these two mechanisms that Java is endowed with powerful object-oriented capabilities. Abstract class and interface have a great similarity in support of the definition of abstract classes, and can even be substituted for each other, so many developers are more relaxed about the choice of abstract class and interface when they are defining abstractions. In fact, there is a big difference between the two, for their choice even reflects the nature of the problem areas of understanding, the design of the intention to understand whether the correct and reasonable. This paper will analyze the differences between them and try to give developers a basis for choosing between them.

Understanding abstract classes and interface are all used in the Java language for abstract classes (the abstract classes in this article are not translated from abstract class, which represents an abstract body, and abstract Class is a method for defining abstract classes in the Java language, which readers should be aware of to differentiate, so what is an abstract class, and what benefits can be derived from using abstract classes?

In the object-oriented concept, we know that all objects are depicted by a class, but that is not the case in turn. 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 that we analyze and design in the domain of problems, and are abstractions of a series of specific concepts that look different, but are essentially the same. For example: If we do a graphic editing software development, we will find that there are problems in the field of circles, triangles and some specific concepts, they are different, but they all belong to the shape of a concept, the concept of shape in the problem area does not exist, it is an abstract concept. It is because abstract concepts do not have specific concepts in the domain of the problem, so abstract classes that represent abstract concepts cannot be instantiated.

In the object-oriented domain, abstract classes are primarily used for type concealment. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can have any possible concrete implementations. This abstract description is an abstract class, and any possible concrete implementations of this group represent all possible derived classes. A module can manipulate an abstract body. Because the module relies on a fixed abstraction, it can be disallowed, and by deriving from this abstraction, you can extend the function of the module. Readers familiar with OCP must know that abstract classes are the key to achieving one of the core principles of object-oriented design OCP (open-closed principle).

viewing abstract class and interface from the perspective of grammatical definition

At the syntactic level, the Java language gives different definitions of the abstract class and interface, and the following is an example of how to define an abstract class named demo.

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

abstract class Demo {
  abstract void method1();
  abstract void method2();
  …

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

interface Demo {
  void method1();
  void method2();
  …
}

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

From a programmatic point of view, abstract class and interface can be used to implement the idea of "design by contract". But there are some differences in the use of specific.

First, 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. 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 default behavior of the method. But in the definition of interface, the method does not have the default behavior, and in order to circumvent this limitation, a delegate must be used, but this can add some complexity and sometimes cause a lot of trouble.

There is another serious problem with the inability to define default behavior in an abstract class, which can cause maintenance problems. Because if you later want to modify the interface of the class (usually represented by abstract class or interface) to adapt to the new situation (for example, adding new methods or adding new parameters to the used methods can be very cumbersome and may take a lot of time (for a lot of derived classes , particularly so). However, if the interface is implemented through abstract class, it may be necessary to modify the default behavior defined in the abstract class.

Similarly, if you cannot define the default behavior in an abstract class, it causes the same method implementation to appear in each derived class of the abstract class, violating the "one Rule,one place" principle, causing code duplication, which is also detrimental to future maintenance. Therefore, it is very careful to choose between the abstract class and the interface.

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.