The contrast between the [base] Java interfaces and abstract classes

The contrast between the Java abstract classes and interfaces

First, the interface

Interface, Translated into the socket it might be better understood. It is typically used to define the behavior of the interface implementation class, when you connect the socket on the laptop triangle unplug, plug in to replace the microwave oven, you will find these two things which are triangular plug. Then the plug triangle can be regarded as a rule, but these two appliances is to achieve the two members of the same rules. Because the rules to achieve the same, so that a dynamically replaced by another member a member becomes much easier. So in the code is the same truth, when the two classes implement the same interface, the client in the original implementation class into another, it becomes easier.

First, the following code defines an interface:

[public] interface InterfaceName {

    void fun1();

}

Interfaces can have variables and methods, but variables defined in the interface is implicitly default is public static finalvariable, and can only be so, if you use other modifier modified, would compile-time error. Similarly, the default method will be public abstractmodified. However, you can only define a method, but can not have implementation class method, the method is the interface can only abstract method, which is on a different and abstract classes (abstract class can have methods to achieve).

And the interface implementation class corresponding to the following example code

public class ClassName implements InterfaceName, OtherInterface, [...] {
    
    public void fun1() {
        // do something ...
    }
    
}

In Java, the class is a single inheritance, but can inherit multiple interfaces, and the interface can inherit multiple interfaces.

Second, the abstract class

In the Java language, there are two categories: one is the concrete class, and the other is an abstract class. Specific class can be instantiated, not abstract class is instantiated. So here you can think of, create an abstract class out, is to be inherited, after all, you can not use a class can not be instantiated to accomplish what functions for you (of course this does not include call static variables and methods, but if you just use to do that, then why do you want this class is declared as abstract it?)

Before understanding abstract classes, we first understand the abstract methods: abstract method is a special method, it is only a statement, but no concrete realization:

abstract void fun1();

In the abstract methods draw class definition must use absractthe keyword modifications, the same abstract class also needs to be abstractkeyword modified

[public] abstract class AbstractClassName {
    
    abstract void fun1();
    
}

If a class inherits the abstract class, subclass it is necessary to implement all the abstract methods defined in the abstract class, unless the subclass is also defined as an abstract class.

Third, the comparison

Contrast grammatical function

  • Interface can contain abstract methods, and ordinary methods can abstract class, an abstract method can have
  • Interface does not contain constructors, abstract class may contain constructors to prepare extension class inherits
  • Interface code block can not include a static method and a static, abstract class can have static code blocks and static methods
  • Interface can only define a static constant properties, both abstract class can define common attributes, you can also define static constants property

Comparison of design thinking

We often say verbally: implements an interface, inherits an abstract class. In fact, this sentence has the distinction between interfaces and abstract classes shown.

The interface is considered more of a contract, where the contract is to allow you to achieve. When someone given you an interface, you will by the method defined in the interface to achieve, it is in the realization of a contract. So in fact, the interface is a collection of features of the method, which method is characterized from course to particular methods, but they are generally in the system some methods from emerging. Only one interface feature of the process, but there is no implementation of these methods in different places to be when implemented, can have different behavior.

The more abstract class is regarded as a template, it provides a starting point inherited, it is focus on achieving the common parts of the subclass. So abstract class is a template-style design, what is a template design? The most simple example, we have used inside ppt templates, if designed ppt B and ppt C A template, ppt B and ppt C common template A part is, if they are part of the public need to change, you only need to change a template can, and do not need to re-ppt B and ppt C to make changes. That is for the abstract class, if you need to add new methods can be added directly in the concrete realization of the abstract class, subclass can not be changed; and for the interface is not, if the interface has been changed, all the realization of this interface class must make the appropriate changes.

Fourth, inherited choose between reuse and norms to achieve

Interface is a special kind of abstract classes, then in development, when the choice of interfaces? When the choice of abstract class it?

  • Preferred Interface

When all of the following conditions are satisfied, the choice of an abstract class

  • Subclass is a special class of the parent class, rather than a parent role, which is to distinguish between two different relationships "Has-A" and "Is-A". Has-A relationship should be described using the aggregation relationship, but only Is-A relationship is consistent with inheritance.

  • You will not need to subclass into another situation subclass of a class

  • Subclass has a responsibility to extend the parent class. Instead of having a replacement out (Override) or written off responsibility (Nullify) the parent class. If the child class needs a large number displaces the behavior of the parent class, then this should not become a subclass of this subclass the parent class.

  • Only when the parent and child classes belong to the same classification before they can use inheritance, do not inherit from the tools.

Five, Java 8's new features

Starting with Java 8, the interface can also have a default method to achieve this is because they do not support the default interface method maintenance costs are too high. Before Java 8, an interface if you want to add a new method, then to modify all classes that implement the interface.

public interface InterfaceExample {

    void func1();

    default void func2(){
        System.out.println("func2");
    }

}
public class InterfaceImplementExample implements InterfaceExample {
    @Override
    public void func1() {
        System.out.println("func1");
    }
}
public static void main(String[] args) {
    InterfaceExample example = new InterfaceImplementExample();
    example.fun1();
    example.fun2();
}

Sixth, reference

  • "Java and model,"
  • https://www.cnblogs.com/felixzh/p/5938544.html
  • https://www.cnblogs.com/devinzhang/archive/2011/12/24/2300260.html
  • https://github.com/CyC2018/CS-Notes/blob/master/notes/Java%20%E5%9F%BA%E7%A1%80.md#%E6%8A%BD%E8%B1%A1%E7%B1%BB%E4%B8%8E%E6%8E%A5%E5%8F%A3

Guess you like

Origin www.cnblogs.com/jojop/p/11334119.html