Java programming things 70 -- abstract classes and interfaces (3)

Source: Internet
Author: User

Java programming things 70mso-hansi-font-family: calibri "> -- abstract classes and interfaces (3)

Chen yuefeng

From: http://blog.csdn.net/mailbomb

8.9.3 Cambria; MSO-Hansi-font-family: Cambria "> comparison of abstract classes and interfaces

Abstract classes and interfaces are dedicated design structures for object-oriented design. In actual project design, you often need to consider the question of "using abstract classes or interfaces "? The following is a simple comparison between abstract classes and interfaces to familiarize themselves with the differences and connections between them, so as to use an appropriate structure in actual design.

Calibri; MSO-bidi-font-family: calibri "> 1.
When can I use abstract classes or interfaces?

The abstract class or interface is used when the structure (MSO-Hansi-font-family: calibri "> is the behavior of the class) declared for the methods in the design. That is, when the subclass is forced to provide a uniform method declaration to the outside, the abstract class or interface is used.

Calibri; MSO-bidi-font-family: calibri "> 2.
Differences between abstract classes and interfaces (Differences)

A) abstract classes are classes, while interfaces are interfaces.

Because an abstract class is a class, the content (MSO-Hansi-font-family: calibri "> constructor, method, and attribute) that can be contained in the class can exist in the abstract class, of course, abstract classes are also limited by the single-inheritance of classes. The interface is an interface type, so the interface can only contain constant attributes and abstract methods, but a class can implement multiple interfaces, so the interface is not limited by the single inheritance of the class.

B) An abstract class can contain entity methods, but an interface cannot.

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri "> An abstract class is a class, therefore, an abstract class can contain both abstract methods and object methods, and each method within an interface must be an abstract method.

C) abstract classes can inherit classes, but interfaces cannot

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri "> An abstract class is a class, so the abstract class can inherit other classes during design, it is designed on the basis of existing classes, but interfaces cannot inherit classes.

Calibri; MSO-bidi-font-family: calibri "> 3.
Abstract class and interface connection (Similarities)

A) abstract classes and interfaces can both declare objects, but they can only be created using the constructor of subclasses.

B) abstract classes and interfaces can both contain abstract methods.

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri "> according to the syntax of javamso-Hansi-font-family: calibri">, when a subclass inherits an abstract class or implements an interface, it must overwrite these abstract methods. Otherwise, it must be declared as an abstract class.

C) abstract classes and interfaces can both represent one type, so as to unify the types of subclass objects and achieve good scalability.

Calibri; MSO-bidi-font-family: calibri "> 4.
When to use abstract classes?

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri "> when the following conditions are met, it is best to use an abstract class for design:

A) The subclass does not inherit other parent classes.

B) The subclass has the same function implementation method.

C) the same attributes exist in the subclass.

D) The designed structure must inherit other classes.

MSO-ascii-font-family: calibri; MSO-Hansi-font-family, you can only use abstract classes. Otherwise, you can use interfaces to implement them.

Calibri; MSO-bidi-font-family: calibri "> 5.
When do I use interfaces?

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri "> when the following conditions are met, it is best to use interfaces for design:

A) The subclass has inherited other parent classes.

B) The subclass does not have the same function implementation method.

C) The subclass does not have the same attribute.

D) The designed structure does not need to inherit other classes.

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri "> when the amso-Hansi-font-family: calibri condition is required, you can only use interfaces. Otherwise, you can use abstract classes.

Calibri; MSO-bidi-font-family: calibri "> 6.
Other uses of abstract classes and interfaces

A) You can declare this class as an abstract class when creating an object of this class is prohibited.

B) You can use interfaces when you need to store a large amount of constant data that will be used between multiple classes in the project.

C) You can use interfaces to unify objects of classes with certain functions. For exampleSerializableCalibri; MSO-Hansi-font-family: calibri; MSO-bidi-font-weight: bold "> interface.

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri; MSO-bidi-font-weight:
Bold "> of course, only after a large number of system design training can we have a deeper understanding of the differences and connections between abstract classes and interfaces, so that we can make choices more freely.

MSO-ascii-font-family: calibri; MSO-Hansi-font-family: calibri; MSO-bidi-font-weight:
Bold "> In addition, it should be noted that not every project must use abstract classes or interfaces.

 

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.