What is the difference between an interface and an abstract class? (1)

Source: Internet
Author: User
Tags define abstract

Abstract class and interface support the definition of abstract classes in Java. The existence of these two mechanisms gives Java powerful object-oriented capabilities. Abstract class and interface have great similarity in support for the definition of abstract classes, and can even be replaced with each other, therefore, when defining abstract classes, many developers may choose abstract classes and interfaces at will. In fact, there is a big difference between the two. Their choices even reflect the understanding of the nature of the problem domain, and whether the understanding of the design intent is correct and reasonable.

 

Abstract class

Interface

Instantiation

No

No

Class

An inheritance relationship. A class can only use an inheritance relationship once. Multiple inheritance can be implemented by inheriting multiple interfaces.

One class can implement multiple interfaces

Data Member

You can have your own

Static objects cannot be modified, that is, they must be static final.

Method

Private and non-abstract methods are required

Private Information is not allowed. The default value is public and abstract.

Variable

Private. The default value is friendly. The value can be redefined in the subclass or assigned again.

Private is not allowed. The default value is the public static final type, and the initial value must be given. The implementation class cannot be redefined or its value cannot be changed.

Design Concept

Indicates the "is-a" relationship.

Indicates the like-a relationship.

Implementation

To be inherited, use extends

Use implements

Abstract class and interface are used for abstract classes in Java language (abstract classes in this article are not translated from abstract class, it represents an abstract body, abstract class is defined as a method used to define abstract classes in Java. So what are abstract classes and what are the benefits of using abstract classes?

The class that declares a method rather than implementing it is called abstract class. It is used to create a class that reflects some basic behaviors and declare a method for this class, however, this class cannot be implemented in this class. You cannot create an abstract instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes. Instead, implement this method in the subclass. Other classes that know their behavior can implement these methods in the class.

An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. All methods in the interface are abstract, and none of them have a program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.

Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces. abstract classes can inherit object classes, provided that the object classes must have clear constructors. The interface is more concerned with "what functions can be implemented", regardless of "how to implement ".

1. Similarities
A. Both are abstract classes and cannot be instantiated.
B. The interface implementation class and the subclass of the objective CT class must implement the declared abstract method.

2. Differences
A. The interface must be implemented and implements must be used. abstract class must inherit and extends must be used.
B. A class can implement multiple interfaces, but a class can inherit only one abstract class.
C. Interface emphasizes the implementation of specific functions, while abstract class emphasizes the ownership relationship.
 
D. Despite the interface implementation class and CT
The subclass of class must implement the corresponding abstract method, but the implementation form is different. Every method in the interface is an abstract method and is declared only.
(Declaration, no method body), the Implementation class must be implemented. Abstract class sub-classes can be selectively implemented.
This option has two meanings:
First, not all methods in the abastract class are abstract. Only those methods with abstract information are abstract. sub-classes must be implemented. Methods without abstract must be defined in objective CT class.
Second, when the subclass of abstract class inherits from it, non-abstract methods can be directly inherited or overwritten. For abstract methods, you can choose to implement them, you can also declare its method as an abstract method, without implementation, and leave it to its subclass for implementation. However, this class must also be declared as an abstract class. It is an abstract class and cannot be instantiated.
E. abstract class is the intermediary between interface and class.
 
The interface is completely abstract. Only methods can be declared, and only pulic methods can be declared. Private and protected methods cannot be declared, and method bodies cannot be defined.
Instance variables cannot be declared. However, the interface can declare constant variables, and it is not difficult to find such an example in JDK. But putting constant variables in the interface violates its
The purpose of the interface also obfuscated the different values of interfaces and classes. If necessary, you can put it in the corresponding abstract class or class.
 
Abstract class plays an important role in interface and class. On the one hand, abstract
Class is abstract and can declare abstract methods to standardize the functions that must be implemented by subclass. On the other hand, it can define the default method body for the subclass to use or override directly. In addition, it can also define itself
To be used by the subclass through inheritance.

3. Application scenarios of interfaces
A. the class and the class need to coordinate with specific interfaces, regardless of how they are implemented.
B. It can exist as an identifier that can implement specific functions, or it can be a pure identifier that does not exist in any interface method.
C. You need to regard a group of classes as a single class, and the caller only contacts this group of classes through interfaces.
D. a specific number of functions are required, and these functions may be completely unrelated.

4. Application scenarios of abstract class
In a word, you can use a unified interface, instance variables, or default methods. The most common ones are:
A. defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. Abstract class can be used to define a set of method bodies, or even empty method bodies, and then the subclass will select the methods that interest it to override.
B. In some cases, the pure interface alone cannot satisfy the coordination between classes. In addition, the variables in the class that indicate the State must be used to distinguish different relations. Abstract's mediation can well meet this requirement.
C. standardizes a set of methods for mutual coordination. Some of these methods are common and independent of the State. They can be shared without sub-classes; in other ways, each subclass needs to implement specific functions based on its own specific State.

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.