Java basics: abstract classes and interfaces, and Basic java Abstract Interfaces

Source: Internet
Author: User
Tags export class

Java basics: abstract classes and interfaces, and Basic java Abstract Interfaces

Reprinted with the source: jiq •'s technical Blog

I. Introduction

Based on the following two principles in the five object-oriented principles, we should consider using interfaces and abstract classes:

Lee's replacement principle:Subclass can replace the parent class by implementing the parent class interface. Therefore, the parent class should be implemented as an interface/abstract class as much as possible to facilitate program expansion without affecting the existing code.

Dependency inversion principle:Unlike structured design, we need to make the bottom layer depend on the top, rather than the opposite.


When designing interfaces and abstract classes, we try to follow the "interface isolation principle" in the five object-oriented principles, that is, it is better to use specialized interfaces than to use a single interface. This rule corresponds to the "single Responsibility Principle" for Class design.


Note: There are many design patterns related to interfaces and abstract classes, such as abstract factory and template methods.


Ii. Concepts

Abstract class(AbstractClass)

(1) abstract classes provide general structures and Prevent Users From instantiating them;

(2) Although abstract classes cannot be instantiated, they still have constructors. ------ Why ???

(3) If you inherit from an abstract class, you have only two options: Implement all abstract methods in the base class; declare yourself as an abstract class;

(4) If you want to rewrite all the export classes or implement a method, you can declare it as an abstract forced export class to implement it.

(5) java does not allow multiple inheritance of classes, but multiple interfaces can be implemented. The interface can be inherited multiple times.


Interface(Interface)

(1) interfaces and internal classes provide us with a more structured method to separate interfaces from implementations.

(2) An interface is a completely abstract class and does not provide any specific implementation;

(3)Interface variable: Implicit isPublic,StaticAndFinalIs a complete constant. It cannot be changed after initialization, and there is only one copy in the memory;

(4)Interface Method:The default value isPublicThe constructor (including) of the common class is protected by default ).

(5)Interface itself: The package access permission is used by default.Protected(Inherited access permissions );But onlyPublic & abstractTo modify an interface.

(6)Multiple inheritance:Different from the class, the interfaceAllow multiple inheritance, An interface can inherit multiple interfaces at the same time.

(7) The interface itself can only be declaredPublic & abstractAnd other modifiers (private, protected, static, final, transient & volatile) are not allowed.


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.