Distinguish abstract classes from interfaces and abstract differences

Source: Internet
Author: User

Distinguish abstract classes from interfaces and abstract differences

The similarities and differences between the two are compared for clarity.

I. Differences between the syntax and the actual application

Abstract class:

Abstract: The class is an abstract class. abstract objects cannot be created.

Abstract classes do not need to be abstract. abstract methods defined in abstract classes must be implemented by subclasses, that is, rewriting.

Therefore, there cannot be abstract constructor methods or abstract static methods. If the subclass does not implement the abstract method of the parent class,

Then, the subclass must also be defined as an abstract class.

Interface:

To a certain extent, an interface can be a special case of an abstract class.

All methods in the interface must be abstract. The method definition in the interface is public abstract by default.

The variables in the interface are modified as global variables, that is, public static final.

In short, the syntax difference is:

1. There can be constructor in the abstract class, and there cannot be constructor in the interface.

2. abstract classes can contain common member variables, but interfaces cannot contain common member variables.

3. abstract classes can contain non-Abstract Common methods, while all methods in interfaces must be abstract.

4. The access type of abstract methods in the abstract class can be public, protected, or default, but the abstract methods in the interface

It can only be of the public type and is of the public abstract type by default.

5. the abstract class can contain static methods, but not interfaces.

6. the abstract class and interface can both contain static member variables. The access type of static member variables in the abstract class can be random,

But the static member variables in the interface can only be public static, and the default value is public static.

7. A class can implement multiple interfaces, but can inherit only one abstract class.

Differences between the two in application:

Interfaces play a role in the system framework design method and mainly define the communication between modules. abstract classes play a role in code implementation to achieve code reuse.

2. simply feel the difference between the two from an instance

1/** Door class */2 abstract class Door {3 4 abstract void open () {}; 5 6 abstract void close () {}; 7}
1/** an Alert interface */2 interface Alarm {3 4 void alarm (); 5 6}
1/** class with the alert function */2 class AlarmDoor extends Door implements Alarm {3 4 void open (){...} 5 6 void close (){...} 7 8 void alarm (){...} 9 10}

The above simple example can intuitively understand the usage of abstract classes and interfaces, and such an implementation method can clearly reflect the correct understanding of the problem.

In fact, abstract class represents "is a", and interface represents a "like a" relationship. You can use it as a basis for your selection, of course, this is based on the understanding of the problem field. For example, if we think that AlarmDoor is essentially an alarm and has the Door function, then the above definition method will be reversed.

Abstract class and interface are two methods of defining abstract classes in Java. They have great similarity. However, their choices often reflect the understanding of the essence of concepts in the problem field, and whether the reflection of design intent is correct and reasonable, because they represent different relationships between concepts (although they all implement the required functions ). This is also a common use of language.

To sum up a few words:

1. abstract classes and interfaces cannot be directly instantiated. to instantiate them, the abstract class variables must point to subclass objects that implement all abstract methods, and the interface variables must point to class objects that implement all interface methods.

2. abstract classes must be inherited by quilt classes, and interfaces must be implemented by classes.

3. The interface can only be used for method declaration. The abstract class can be used for method declaration or method implementation.

4. the variables defined in the interface can only be public static constants. the variables in the abstract class are common variables.

5. the abstract methods in the abstract class must be implemented by all the quilt classes. If the subclass cannot all implement the parent class abstract methods, the subclass can only be an abstract class. Similarly, if an interface cannot be fully implemented, the class can only be an abstract class.

6. abstract methods can only be declared and cannot be implemented. Abstract void abc (); cannot be written as abstract void abc (){}.

7. There can be no abstract methods in the abstract class.

8. If an abstract method exists in a class, the class can only be an abstract class.

9. abstract methods must be implemented, so they cannot be static or private.

10. An interface can inherit an interface and inherit multiple interfaces, but a class can only inherit one interface.

Abstract classes have their advantages, especially for public implementation code. Abstract classes can ensure implementation hierarchies and avoid code duplication. However, even when using image extraction classes, do not ignore the principle of defining behavior models through interfaces. From a practical point of view, if you rely on abstract classes to define behavior, it often leads to overly complex inheritance relationships, and defining behavior through interfaces can effectively separate behavior and implementation, it facilitates code maintenance and modification.

 

For more information, see [http://www.cnblogs.com/azai/archive/2009/11/10/1599584.html.

Related Article

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.