Interfaces and abstract classes in Java

Keywords: Java

1, Introduction

Abstract class: a class modified by abstract can be called an abstract class. Abstract classes can have abstract methods, that is, methods decorated with abstract. Such methods have no specific implementation structure, but only a definition.

Interface: a class like structure formed by replacing class with interface. The methods in the interface are all abstract methods, and the interface itself does not realize any functions.

2, Abstract class

Abstract class is still a class in essence, but it has one more feature: it can define abstract methods in abstract classes. In addition, abstract classes cannot be instantiated, that is, it cannot create an object running type bit abstract classes. See the following example:

 1 class ceshi{
 2     public static void main(String[] args) {
 3         animal a1 = new animal();//report errors
 4         Dog a3 = new Dog();
 6     }
 7 }
 8 abstract class animal{
 9   public void speak() {
10         System.out.println("this is an animal");
11     }
12     public abstract void run();
13 }
14 class Dog extends animal{
15     @Override
16     public void run() {
17         System.out.println("Dog is running");
18     }
19 }       

You can see that animal is an abstract class, which has an abstract method run. Dog class inherits the abstract class animal and implements the abstract method run. Here are some points to note:

1. Abstract class animal can have its own implemented methods, such as the speak method in line 9. Objects that inherit the dog class of abstract class animal can directly call this speak method, which is the same as ordinary inheritance. Dog can also override the speak method himself.

2. There can be no abstract methods in an abstract class, and the compiler will not report an error. However, if there are abstract methods, the ordinary class inheriting this abstract class must implement all abstract methods, or an error will be reported. If not implemented, you can also define yourself as an abstract class.

3. Abstract classes cannot be instantiated, that is, they cannot appear after new, and the compiler cannot allocate space for an abstract class. Error reporting in the third line of the above example:


It clearly shows that the abstract class modified by abstract cannot be instantiated.

3, Interface

There is a sentence in java programming thought: "the interface keyword makes the abstract concept a step forward".

Any method in the interface is abstract. The keyword interface produces a completely abstract class. It does not provide any concrete implementation at all (the above abstract class allows to have its own method), and only allows the creator to determine the method name, parameter list and return class type. One interface can make all the classes that implement it look consistent.

Let's also look at an example:

 1 class ceshi{
 2     public static void main(String[] args) {
 3         animal a1 = new animal();//report errors
 4         Dog a3 = new Dog();
 5         a3.speak();
 6     }
 7 }
 9 interface animal{
10   public void speak() ;
11   public abstract void run();
12 }
13 class Dog implements animal{
14     public void speak() {
15        System.out.println("this is an Dog");
16     }
17     @Override
18     public void run() {
19         System.out.println("Dog is running");
20     }
22 }

It can be seen that I have transformed animal from an abstract class to an interface, which is also implemented by the Dog class (the abstract class is called inheritance). There are also several points to pay attention to:

1. In fact, the methods in the interface have three keywords by default: abstract, static and final, which exist unconditionally in the interface. Therefore, pay attention to the comparison between lines 10 and 11. The abstract in line 11 is redundant, and my compiler also reminds me:


2. The class that implements the interface must also implement the abstract methods, and the methods in the interface are abstract, so you must implement all the methods defined in the interface.

3. There can only be abstract methods in the interface, and methods with method body are not allowed.

4. Similarly, the interface cannot be instantiated, that is, the error is reported in the third line. Let's see the reason for the error:


This may be a little familiar. When we try to instantiate an abstract class, it is also a hint that the interface is actually an abstract class by default, but it is more abstract.


Can constants exist in the interface? In fact, it is possible:

1 public static void main(String[] args) {
2         System.out.println(animal.x);
3     }
5 interface animal{ 
6     int x = 0;
7   public void speak() ;
8   public abstract void run();
9 }

For example, I defined a constant in the animal interface. Although I only defined it, as mentioned above, x is a static variable modified by static and final by default (abstract can only modify methods and classes), so I can use the name of the interface in the main function to call the value of x, which is similar to the static variable in ordinary classes, The interface also has its static storage space, and static variables are stored in it.

3, Comparison between interface and abstract class

Finally, post a summary:



Posted by seansd on Thu, 02 Dec 2021 16:19:26 -0800