Abstract Class and Interface difference, abstractinterface

Source: Internet
Author: User

Abstract Class and Interface difference, abstractinterface
Table

 

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

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.

Abstract class application scenarios

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.

Analysis

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.

I am the dividing line of tiantiao

 

 

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.