Java foundation fifteen (abstract classes, interfaces)

Abstract class

We define some subclasses are found with the same behavior you can use these same behaviors to extract parent class has a problem is that these acts only to be implemented by concrete subclasses, but in a parent class, not in line with the basic logic, we can not generalize, so the parent class among the concrete realization of this function can not be, can not have the specific function of the body, so to talk about the function is defined as an abstract function, a function of how they implement the subclass, heavy written decision. Then, based on the abstract function called abstract class

When defining the function of the abstract class must also be modified abstract keyword, keyword modified abstract class is an abstract class.

Features abstract class

  1. Abstract classes and abstract methods need to be modified abstract. Abstract method must be defined in an abstract class.
  2. An abstract class can not create the object because: call the abstract method does not make sense.
  3. Only covers all the abstract methods abstract class that subclasses can be instantiated. Otherwise, the sub-class or an abstract class.
  4. When a subclass inherits from the abstract class, if not completely rewrite the abstract function, then this class must be abstract
  5. The reason why inheritance, more so in thought, in the face of common type of operation would be simpler

Spread

  • An abstract class must be the father? Yes, because it is constantly drawn it come.
  • An abstract class must be abstract function? Can not, but this is the meaning of existence is just an abstract class is to keep the class to create the object.
  • Are abstract class have a constructor? There are, though not to their object initialization, but can be initialized to the object subclasses.

Abstract class similarities and differences with the general class:

  1. the same:

    它们都是用来描述事物的
    它们之间都可以定义属性和行为
    
  2. different:

    一般类可以具体的描述事物,抽象类描述事物的信息不具体
    抽象类中可以多定义一个成员:抽象函数
    一般类可以创建对象,抽象类不能创建对象
    

Keywords abstract abstraction can not co-exist with those keywords

  1. final: when the final modified class can not be inherited, and modified abstract class must have subclasses. final modification methods can not be covered, and abstract method must be modified to be implemented by subclasses
  2. static: Static methods belong to the class of modification, it is a static method in the area, and there is no relationship between the objects, and abstract method has no method body, invoked through the class name it makes no sense
  3. private: private method can not be inherited, there was no coverage, while abstract and private modification methods together, to abstract subclass to implement this method, and private modification, subclasses can not get the parent class method, contradictory

interface

The origin of the interface
if the method of abstract class full of abstract, this abstract class can be used to express the interface, the interface is a special abstract class.

Defining an interface
defined class or an ordinary class abstract class can use the keyword, the keyword interface defines the interface to be completed, i.e., replaced with the interface class, this time between classes and interfaces can be implemented relation, and the interface at this time can not as a class to look at the

interface class Demo
{
        abstract void show1();
        abstract void show2();
}

Features members of interfaces

  1. Interface can define a variable, but must have a fixed variable modifiers modified, public static final variables interface so called constant.
  2. Interfaces may define methods, methods have fixed modifiers, public abstract
  3. Interface members are public.
  4. Interface can not create object.
  5. Subclasses must override the interface all abstract methods, subclasses can be instantiated. Otherwise sub-class is an abstract class

Interface solve the drawbacks of multiple inheritance
multiple inheritance drawbacks: when multiple inheritance, when more than one parent class has the same functionality, subclasses call will create uncertainty. In fact, the core reason is that multiple inheritance function in the parent class with a main body, an invocation operation, which the body content (ambiguous) to run indefinitely.
This interface multiple inheritance mechanism to achieve more than done in java through, why achieve more than to solve it?
Because the interface method body functions are not, by subclasses to clear

Interface: 1 add features as an intermediate transfer type 2 (Specification Protocol)

1, to achieve more than the relationship between classes and interfaces: to pay more functional class, subclass must generate relations and interfaces, the relationship between class and class is inherited, the relationship between the class and the interface is implemented. By keyword implements

2, class inheritance class implements the interface
subclass inherits the parent class by extension, extended functionality through inheritance are subclass should have the basic functions (some a priori). If you want to continue to expand the subclass functions of other classes it? Then by implementing the interface to complete (acquired added) interface to avoid the emergence of a single inheritance limitations. The basic function of things in the parent class definition. Extension of things defined in the interface.

3, interface with multiple inheritance
can be used between multiple interfaces extends inheritance, leaving only the function declaration does not specify a specific implementation, so no ambiguity

4, in the same manner as if the development of multiple interfaces, then if a class implements these interfaces, the interface methods will be realized, since the interface method is an abstract method, does not achieve the subclass the uncertainty of an invocation

Interface advantages in development

  1. It extends the functionality of the interface appears.
  2. The interface is actually escaping violent rule.
  3. Reduced occurrence coupling interface, i.e. to achieve a decoupling between the device and the device.
    Interface appears easy to use and maintain the latter, one is using the interface (such as computers), a party in the implementation of the interface (the device inserted in the socket). For example: notebook to use this rule (Interface), computer peripherals implement this rule (Interface)
The difference between abstract classes and interfaces

Same point:

They are located in succession to the top, or by other implementations for inheritance;
; can not be instantiated (not create object)
These methods include abstract abstract methods that subclasses must override;

the difference:

Abstract classes implemented as part of the method, to avoid repetition subclasses implement these methods provide code reuse; interface can contain abstract methods;
a class can inherit one direct superclass (which may be an abstract class), but they can implement multiple interfaces ; (Java interfaces make up the single inheritance)

Both selection:

Preferred interface to minimize the use of abstract classes;
when required behavior defined subclasses, but also to provide common functionality to a subclass of abstract class selection

Published 70 original articles · won praise 56 · views 1995

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/103425054