Do you remember the characteristics of abstract classes and interfaces in Java?

Preface

Abstract classes and interfaces are often asked at the same time in interviews. There are certain similarities and differences between the two, which can easily lead to confusion. I also remembered to forget, I forgot to review it, and I forgot after a while. So I wrote this article today to summarize the characteristics and differences between the two, and consolidate my Java knowledge system by the way, and it is also convenient for future review. I will elaborate as much as possible about the respective characteristics of the two, if any omissions, please add.


Abstract class

  1. Abstract classes belong to classes, which are classmodified by keywords, single inheritance, and also abstractmodified by keywords;
  2. A class with abstract methods is an abstract class, but an abstract class can have no abstract methods;
  3. Abstract classes can have constructors, but they cannot be instantiated, that is, objects cannot be created by new. But you can still define the variables of the abstract class to point to the subclass implementation and achieve polymorphism;
  4. Abstract classes can have concrete implementations of non-abstract methods; abstract methods in abstract classes have only method bodies, not implementations;
  5. If the subclass inherits the parent class of the abstract class, all of its abstract methods must be rewritten, otherwise the subclass can only be declared as an abstract class and cannot be instantiated;

interface

  1. The interface is interfacemodified by keywords and implemented more;
  2. Interface can be inherited by interface, using extendskeywords (the same as class inheritance); it can also be inherited by class, using implemetskeywords;
  3. The methods in the interface are all abstract methods, and all must be implemented by subclasses that inherit the interface;
  4. The interface is public, there are no private methods or variables, that is, no privatekeywords will appear ;
  5. The member variables in the interface are public static finalmodified by default and must be assigned initial values; all abstract methods are modified by publicand abstract;

to sum up

An "interface" is an "abstract class" that is more "abstract" than an "abstract class" . There can be concrete implementations of non-abstract methods in an abstract class, and all interfaces are abstract methods. Neither abstract class nor interface can be instantiated. Both of them are implemented by their subclasses to implement their internal abstract methods. Both are "abstract" manifestations. If the abstract method is declared, it is good to let its subclass implement the corresponding function for the method. La.

What is often called " interface-oriented programming " now is that in object-oriented thinking, let the interface define a set of rules, and its subclasses only need to consider the specific implementation.

Guess you like

Origin blog.csdn.net/qq_45590494/article/details/106007005