What is the difference between Java Abstract class and interface?

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. Interface implementation class and CT
Class subclasses must implement declared abstract methods.

2. Differences
A.
The interface must be implemented and implements must be used. abstract class must be inherited 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 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,
There is no method body), the Implementation class must be implemented. Abstract class sub-classes can be selectively implemented.
This option has two meanings:
First, abastract
Not all methods in class are abstract. Only those methods with abstract titles are abstract, and subclass must be implemented. Those methods that do not abstract, in CT
The method body must be defined in class.
Second, abstract
When a subclass of A Class inherits from it, it can be directly inherited or overwritten for non-abstract methods. 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.
Interfaces are completely abstract and can only declare methods. They can only declare pulic methods, private and protected methods, method bodies, and instance variables. However, the interface can declare constant variables, and it is not difficult to find such an example in JDK. However, putting constant variables in an interface violates its purpose as an interface and obfuscated the different values of interfaces and classes. If necessary, you can place 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 its own instance variables for subclass to use 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.
As an identifier that can implement specific functions, it can also be a pure identifier that does not exist in any interface method.
C.
Consider a group of classes as a single

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.