Java Basics: Abstract classes and interfaces

Source: Internet
Author: User
Tags export class

First, Introduction

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

Richter Substitution principle: Subclasses can replace the parent class by implementing the parent class interface, so the parent class should be implemented as an interface/abstract class as much as possible, so that the program is easily expanded without affecting existing code.

dependency Inversion principle: Unlike structured design, we want to make the bottom layer dependent on the top, not the other way around.


While designing interfaces and abstract classes, we try to follow the "interface isolation principle" in the five principles of object-oriented, that is, using a specialized interface is better than using a single interface. This guideline corresponds to the "single responsibility Principle" for class design.


Note: There are many patterns associated with interfaces and abstract classes in design patterns, such as abstract factories, template methods, and so on.


Second, the concept

Abstract class (AbstractClass)

(1) abstract classes provide a common structure and prevent users from instantiating them;

(2) Although an abstract class cannot be instantiated, it still has constructors. ------ why???

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

(4) if you want to make all the exported classes replicate or implement a method, you can declare them as Abstract forces the export class to implement it.

(5) Java Multiple inheritance of classes is not allowed, but multiple interfaces can be implemented. interfaces can inherit multiple.


Interface (Interface)

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

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

(3)Interface Variables: Implied is Public,Staticand theFinalof,is a complete constant, after initialization can not be changed, in memory only one copy;

(4) interface method: The default is public Whereas the normal class method ( includes ) Span lang= "ZH-CN" style= "font-family: Arial" > constructor default is package access (protected)

( 5) interface itself : Default is package access rights protected Span lang= "ZH-CN" style= "font-family: Song Body" and ( inherited access Permissions ) but only public & abstract to decorate an interface.

( 6) multiple inheritance: allow multiple inheritance Span lang= "ZH-CN" style= "font-family: Arial", an interface can inherit multiple interfaces at the same time.

(7) the interface itself can only be declared as public& Abstract and, other modifiers (private, Protected, static,final, transient& volatile) are not allowed.


Java Basics: Abstract classes and 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.