当前位置:网站首页>What is interface? What is the difference between interface and abstract class? Case study

What is interface? What is the difference between interface and abstract class? Case study

2021-01-23 22:08:07 ASU, Asia

4 Interface

4.1 Interface

4.1.1 What is an interface

Class is a concrete implementation , And interfaces define a specification , Interfaces define the specifications that a batch of classes need to adhere to , The interface doesn't care about the internal state data of these classes , I don't care about the implementation details of the methods in these classes , It only states that certain methods must be provided in these classes .

Interface embodies the design philosophy of separation of specification and implementation .

4.1.2 Interface features

Definition of interface :

Definition and use of interface interface keyword , instead of class keyword . The grammar is as follows :

[ Modifier ] interface  The interface name  extends  The parent interface 1, The parent interface 2{

}

public abstract interface ISuper extends ITest1,ITest2 {

    public static final String name="xiaoming";

    public abstract void f1();
}

Interface features :

  • All properties in an interface can only be static constants , The default is public static final.
  • All methods in an interface can only be abstract methods , The default is public abstract.
  • The interface is implicitly abstract , When declaring an interface , No need to use abstract keyword ; Every method in the interface is also implicitly abstract , There is also no need for public static final or public abstract.
  • Because the interface defines a specification , So you can't instantiate in an interface 、 Cannot contain constructor and initialization block definitions .
  • stay Java 8 in , Static methods are allowed in interfaces 、 Private method , And abstract methods with default implementations .

Inheritance of interfaces :

  • Interface supports multiple inheritance , That is, an interface can have multiple direct parent interfaces . Multiple parent interfaces are arranged in extends After keyword , Separated by commas .
  • Between multiple inherited interfaces , There can be repeated methods , The signature and return value of a method must be exactly the same .
  • Similar to class inheritance , A child interface extends a parent interface , Will get all the abstract methods defined in the parent interface 、 Constant Field、 Inner class and enumeration class definitions .

Class implementation interface :

  • The syntax of the class implementation interface is as follows :
[ Modifier ] class  Class name  extends  Parent class  implements  Interface 1, Interface 2{

}
  • A class can implement one or more interfaces , Use implements keyword . When you need to implement multiple interfaces , Multiple interfaces are separated by English commas . A class can inherit a parent class , And implement multiple interfaces at the same time ,implements Part of it has to be in the extends After part . A class can implement multiple interfaces , This is also Java A complement to the lack of flexibility in single inheritance .
  • Implementing interfaces is similar to inheriting parent classes , You can also get constants defined in the implemented interface Field、 Abstract method 、 Inner class and enumeration class definitions .
  • After a class implements one or more interfaces , This class must fully implement all the abstract methods defined in these interfaces ( That is to rewrite these abstract methods ); otherwise , This class will keep the abstract methods inherited from the parent interface , The class must also be defined as an abstract class .

4.2. The difference between interface and abstract class

Interface : Interfaces are the combination of implicit public methods and properties , Packaged as a collection of specific functions .

abstract class : Something common is found in subclasses , Generalize the parent class , Then the subclass inherits the parent class , Consider defining a parent class that instantiates nothing as an abstract class .

Interfaces are like abstract classes , They all have the following characteristics :

  • Interfaces and abstract classes cannot be instantiated , They are all at the top of the inheritance tree , For implementation and inheritance by other classes .
  • Both interfaces and abstract classes can contain abstract methods , These abstract methods must be implemented by ordinary subclasses that implement interfaces or inherit abstract classes .

But there's also a big difference between interfaces and abstract classes .

Grammatical differences :

  • Abstract classes can provide implementation details of member methods , And there is only... In the interface public abstract Method ;
  • Member variables in abstract classes can be of various types , The member variables in an interface can only be public static final Type of ;
  • Interfaces cannot contain static code blocks and static methods , Abstract classes can have static code blocks and static methods ;
  • A class can only inherit one abstract class , A class can implement multiple interfaces .

Conceptual differences :

  • Class is an abstraction of an object , An abstract class is an abstraction of a class , Interface is an abstraction of behavior , Abstract classes are used for commonality , The interface is used to specify , Abstract class is to abstract the whole class , Including attributes 、 Behavior , But the interface is local to the class ( Behavior ) Abstraction .
  • A simple example , Airplanes and birds are not of the same kind , But they all have one thing in common , Just can fly . So in design , You can design an airplane as a class Airplane, Design the bird as a class Bird, But you can't take “ flight ” This feature is also designed as a class , Because it's just a behavioral trait , It's not an abstract description of a class of things . Now you can put “ flight ” Designed as an interface Fly, Contains methods fly( ), then Airplane and Bird According to their own needs respectively Fly This interface . And then there are different kinds of planes , Like fighter jets 、 Civil aircraft, etc Airplane that will do , The same is true for birds , Different kinds of birds inherit directly Bird The class can . You can see it here , Inheritance is a “ Is it right? ” The relationship between , And the interface implementation is “ Is there any ” The relationship between . If a class inherits an abstract class , A subclass must be a class of abstract classes , The interface implementation is there or not 、 Relationships you don't have , Like whether birds can fly ( Or whether they have the characteristic of flying ), You can implement this interface if you can fly , You don't implement this interface if you can't fly .
  • formula : Magpies are birds ( Birds make abstract classes ), It has the function of flying ( Flight interface ).

Design level differences :

  • The abstract class ACTS as the parent of many subclasses , It's a template design . An interface is a behavior specification , It's a radial design .
  • What is template design ? Simplest example , You've all used it ppt The template inside , If you use a template A Designed ppt B and ppt C,ppt B and ppt C The common part is the template A 了 , If their common parts need to be changed , You just need to change the template A That's all right. , It doesn't have to be redone ppt B and ppt C change . And radial design , Like an elevator with some kind of alarm , Once the alarm is updated , You have to update them all . That is, for abstract classes , If you need to add a new method , You can add concrete implementations directly to an abstract class , Subclasses can be left unchanged ; Not for interfaces , If the interface changes , All classes that implement this interface must be changed accordingly .
  • Here's one of the most widely Shared examples on the Internet : Examples of doors and alarms : The door has a open( ) and close( ) Two actions , At this point, we can define this abstract concept through abstract classes and interfaces :
abstract class Door { 
    public abstract void open(); 
    public abstract void close();
}
 perhaps :
interface Door { 
    public abstract void open(); 
    public abstract void close();
}
  • But now if we need the door to have an alarm alarm( ) The function of , So how ? Here are two ideas :
    • 1) Put all three functions in an abstract class , But then all the subclasses that inherit from the abstract class have an alarm , But some doors do not necessarily have alarm function ;
    • 2) Put all three functions in the interface , Classes that need to use the alarm function need to implement the interface open( ) and close( ), Maybe this class doesn't exist at all open( ) and close( ) These two functions , Like the fire alarm .
  • You can see it here , Door Of open() 、close() and alarm() There are basically two different categories of behavior ,open() and close() A behavior inherent in the door itself , and alarm() The additional behavior of an extension . Therefore, the best solution is to design the alarm as a separate interface , contain alarm() Behavior ,Door Design as a single abstract class , contain open and close Two kinds of behavior . Design a new alarm door inheritance Door Class and Implementation Alarm Interface .
interface Alram { 
    void alarm();
} 

abstract class Door { 
    abstract void open(); 
    abstract void close();
} 

class AlarmDoor extends Door implements Alarm { 
    void oepn() { //.... }
    void close() { //.... } 
    void alarm() { //.... }
}

版权声明
本文为[ASU, Asia]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123220747934p.html

随机推荐