Summary of Java knowledge points [6] abstract classes and interfaces

1. Abstract class

1) What is an abstract class

The opposite of abstract is concrete, the less concrete, the more abstract. abstract modified class is called abstract class, in addition to not be instantiated! Other grammatical rules are the same as those of ordinary classes.

The meaning of the abstract keyword is to allow the programmer to clearly tell the compiler that this class is an abstract class and should not be instantiated, so the compiler must do a good job of related checks.

2) What is an abstract method

  • Add abstract to the front of the method and this is an abstract method.
  • Abstract methods don't need method bodies .
  • Abstract methods can only exist in abstract classes (and can also exist in interfaces), and cannot exist in ordinary classes.
  • The purpose of abstract methods is to allow subclasses to rewrite.

3) Examples

abstract class Animal{ //抽象类
    public abstract void eat(); //抽象方法
}

public class Blog {
    public static void main(String[] args) {
        Animal animal=new Animal();
    }
}

operation result:

2. Interface

1) What is an interface

We know that there can only be single inheritance between classes. In order to achieve an effect similar to "multiple inheritance", an interface is introduced. The interface is a step closer to the abstract class and is more abstract than the abstract class. Abstract classes just can't be instantiated, but all other aspects are similar to ordinary classes, and the interface is more abstract. Not only can it not be instantiated, but it also doesn't have the various characteristics of the class.

Naming: The naming of the interface is generally prefixed with a capital letter I , and adjectives are generally used for naming. Representation semantics: a class has XXX characteristics

2) Examples

Only abstract methods can be placed in the interface , not ordinary methods (the wavy line in the figure below)

The abstract method in the interface can not write the public abstract keyword, write it or not, it all means the abstract common method~

But the abstract method in the abstract class must be written with the abstract keyword. In addition to the abstract method, you can also put the ordinary method in the abstract class, so if you don't write it well, it means a normal method, and you have to add the method body.

②Only public static final modified attributes can be put in the interface , not ordinary attributes

interface IEating {
    public static final String food = "fish";
}

③There is no inheritance between the interface and the class, only the class implements a certain interface. There can be inheritance between interfaces and interfaces.

Animal class

public class Animal {
    
}

IEating interface

public interface IEating {
    public static final String food = "fish";
    public abstract void eat();
}

IJump interface

public interface IJump {
    void jump();
}

The Cat class inherits from the Animal class and implements the IEating and IJump interfaces

public class Cat extends Animal implements IEating,IJump{
    @Override
    public void eat() {

    }
    @Override
    public void jump() {

    }

}

note:

  • The implemented class must rewrite all methods in the interface.
  • To implement multiple interfaces, use a comma to separate the interfaces.

3. Comparison of abstract classes and interfaces

  • Abstract classes are similar to ordinary classes, except that they cannot be instantiated. The interface is far from the ordinary class (including the properties, methods, and the relationship between other classes)
  • A class can only inherit from one abstract class, but a class can implement multiple interfaces at the same time.

Why invent a grammar like an interface?

Solve the problem of non-multiple inheritance in Java. Inheritance in Java is single inheritance, and multiple inheritance is useful in some scenarios. In Java, you can achieve the effect similar to multiple inheritance by inheriting a class and implementing multiple interfaces.

 

 

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/113096038