The difference and relation between abstract class and interface

Source: Internet
Author: User

1. The same point:
A. Both are abstract classes and cannot be instantiated.
B. Interface implementations and abstract class subclasses must implement an abstract method that has already been declared.

2. Different points:
A. Interface need to be implemented, use implements, and abstract class need to inherit, use extends.
B. A class can implement multiple interface, but a class may inherit only one abstract class.
C. Interface emphasizes the implementation of a particular feature, while the abstract class emphasizes its affiliation.
D.
Although interface implements classes and ABSTRCT
Subclasses of class must implement the appropriate abstract methods, but the implementations are in different forms. Each of the methods in interface is an abstract method, just a declaration of
(Declaration, there is no method body), the implementation class must be implemented. The subclass of abstract class can be implemented selectively. This option has a two-point implication:

One is that not all methods in the Abastract class are abstract, only those whose crown has an abstract are abstracted, and subclasses must be implemented. For those that do not have an abstract method, the method body must be defined in the ABSTRCT class.

The second is that when the subclass of abstract class inherits it, it can either inherit directly from the non-abstract method or overwrite it, but the abstract method can be implemented by choice, or it can be implemented by declaring its method abstract, without implementing it and leaving it to its subclasses, but this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.
E. Abstract class is an intermediary between interface and class.
Interface
is completely abstract, can only declare methods, and can only declare pulic methods, cannot declare private and protected methods, cannot define the method body, Also
Instance variables cannot be declared. However, interface can declare constant variables, and this example is not hard to find in the JDK. However, placing a constant variable in the interface violates its
The role of the mouth and the purpose of existence, but also confuse the interface and the different values of the class. If you really need it, you can put it in the appropriate abstract class or class.
Abstract
Class plays a connecting role in interface and class. On the one hand, abstract
Class is abstract, you can declare abstract methods to standardize the functions that subclasses must implement, on the other hand, it can define the default method body, which is used or overwritten by subclasses directly. In addition, it can also define its own
Instance variable to be used by the subclass for inheritance.

3, Interface application occasions
A. Classes and classes require a specific interface to reconcile, rather than how they are implemented.
B. As an identity that is capable of implementing a specific function, it can also be a purely identifying interface method that does not exist.
C. A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.
D. A specific number of functions need to be implemented, and there may be no connection between these features at all.

4. Application of abstract class

In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:
A. Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.
B. In some cases, a purely interface does not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.
C. Standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, and do not need subclasses to implement separately, while others require individual subclasses to implement specific functions according to their own specific state.

The difference and relation between abstract class and 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.