Abstract classes and interfaces in Java

An abstract class

        An abstract class, to put it bluntly, is an abstraction of a class. Then we know that a class is also an abstraction of a class of things. How to understand an abstract class? I think we should use some examples to illustrate what an abstract class does.

        We first need to know the definition format of an abstract class:    public abstract class class name

        So what do we write inside the abstract class?

       We can define some methods in an abstract class. The methods we define are public abstract by default. The point to remember here is that abstract methods must exist in abstract classes, but abstract classes do not necessarily have to be abstract. method. Some common methods can also be defined. Then when we define an abstract method, we cannot write anything in this abstract method, that is, we cannot implement this abstract method. As shown in the figure below, the program will directly become popular, prompting us that abstract methods cannot have method bodies:

       Since we define an abstract method, the program does not allow to write a specific implementation, so what is the use of defining this abstract method? Here it implies to us that the abstract class must be inherited by other classes, and then the class that inherits it This abstract method is implemented in detail. Let's try it out, define an abstract method in the abstract class Person class, and let the student class Student inherit it. code show as below:

       We rewrite the abstract method in the Person class in the Student class, and then implement the concrete implementation. What we need to remember here is that the abstract methods in the abstract class must be completely overridden in the class that inherits it. The above programs can run normally, and we can also write some common methods in abstract classes, and common methods can be implemented concretely without the need for concrete implementations in other classes. This reduces code duplication. For example, we write a common method eat() for the Person class, the code is as follows:

 The ordinary method in the abstract class does not need to be rewritten in its subclass. Of course, if you want to rewrite it, it is still possible.

       So, what is the use of defining an abstract class, in my opinion (if there is a better understanding than me, please leave a message): when we define an abstract class, we can put the public behaviors and properties of other classes in This is implemented here. For example, in the above example, define a Student class and a Teacher class. The public method of their classes is that they will all eat, so I can implement the method of eating in the Person class. But for the sleep method, only students may have lunch breaks, but teachers may not have lunch breaks, that is, they have different forms for the sleep method, so we can define this abstract method in an abstract class without explaining this method. What to do, what this method does specifically, can be implemented in the class that inherits it.

2. Interface

        Interface is an abstraction of abstraction. I believe that after listening to this sentence, it is like listening to this sentence, and I have no understanding, and I don't know what it means at all. In the interface, the methods we define are only methods without specific implementations. You should feel that way. Let's go further. In our previous example, a sleep abstract method is defined in Person. Although there is no specific implementation, we know that this method must be related to people. Then we define an abstract method in the interface as sleep (sleep) at this time. The sleep here, we don't know whether it is related to people, maybe it is related to animals. Because animals also sleep. Speaking of this, everyone should have a similar understanding . Only abstract methods are declared in the interface, and the declared methods must be abstract, generally the abstraction of a certain behavior. Who will use this method can access this interface. .

       The declaration format of the interface is as follows. When we define a method in it, we do not need to write public and abstract. It reminds us that these two modifications are redundant, because the methods in the interface are abstract public methods by default, as shown in the following figure:    

       In the interface, we can also declare a variable (variables are constants here), which are decorated with public static final in front. These are all defaults. When declaring, there is no need to write them, they are all redundant, as shown in the following figure:

       After we declare an interface, if we want to use this interface in other classes, we can use the implement keyword to access this interface. What we should pay attention to here is that a class can inherit multiple interfaces, which overcomes java The disadvantage of only single inheritance between Chinese classes and classes, multiple inheritance can be achieved in the interface .

Let's define an interface IF, define an abstract class Person, define two ordinary classes Student, Animal, to write an example, first look at the implementation effect:

 The specific code is as follows:

public class Main {
    public static void main(String[] args) {
        Person person=new Student();//实例化类Student的一个对象person,作为Person类的引用。
        IF f=new Animal();//实例化类Animal的一个对象f,作为接口IF类的引用。
        f.run();
        IF f1=(IF) person;//实例化类Student的一个对象person,作为Person类的引用,然后强制转换为IF接口的引用
        f1.run();
    }
}
//声明一个接口
public interface IF {

    public abstract void run();//这里写不写public abstract都行
}
//定义一个抽象类
public abstract class Person {
    public abstract void sleep();//定义的抽象方法
    void eat(){
        System.out.println("人们大多数都是一日三餐");
    }
}
//定义一个普通的类
public class Student extends Person implements IF{
    //这个是对继承的Person抽象类中的方法进行重写,然后进行具体的实现。
    @Override
    public void sleep() {
        System.out.println("学生们都会有午休");
    }
    //当我们继承一个接口时,我们需要对这个接口中的所有抽象方法都进行重写。
    
    //重写接口中的方法run
    @Override
    public void run() {
        System.out.println("学生们跑的很快");
    }
}
//定义一个普通类Animal
public class Animal implements IF {
    //因为该类继承了接口IF,所以要重写IF中的全部抽象的方法
    @Override
    public void run() {
        System.out.println("有些动物跑的比人快");
    }
}

Summary: Abstraction and interface are two things that we can only better understand when we apply them in concrete practice. We just need to remember the rules for defining them and their characteristics.

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/121214098