abstract classes and interfaces

Java interfaces and abstract classes

 

An abstract class

An abstract method is a special kind of method: it has only a declaration, but no concrete implementation. The declaration format of an abstract method is:

abstract void test();

 

Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, the class is called abstract class, and the abstract class must be modified with the abstract keyword before the class. An abstract class cannot be used to create objects because an abstract class contains methods that have no concrete implementation.

 

Pay attention to a problem: in the book "JAVA Programming Ideas", an abstract class is defined as "a class containing abstract methods", but later it is found that if a class does not contain abstract methods, it is also an abstract class if it is only modified with abstract. That is to say, an abstract class does not necessarily have to contain abstract methods. Personally, I think this is a serious problem, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So keep this concept in mind for now, without getting into why.

 

It can be seen from this that abstract classes exist for inheritance. If you define an abstract class, but do not inherit it, then it is equivalent to creating this abstract class in vain, because you cannot use it to do anything. For a parent class, if a certain method of it is implemented in the parent class without any meaning, and must be implemented differently according to the actual needs of the subclass, then this method can be declared as an abstract method, and the class is also It becomes the abstract class.

 

A class that contains abstract methods is called an abstract class, but it does not mean that an abstract class can only have abstract methods. Like ordinary classes, it can also have member variables and ordinary member methods. Note that there are three main differences between abstract classes and ordinary classes:

  1) The abstract method must be public or protected (because if it is private, it cannot be inherited by subclasses, and subclasses cannot implement the method), and the default is public by default.

  2) Abstract classes cannot be used to create objects;

  3) If a class inherits from an abstract class, the subclass must implement the abstract method of the superclass. If the subclass does not implement the abstract method of the superclass, the subclass must also be defined as an abstract class.

 

  In other respects, abstract classes are no different from ordinary classes.

2. Interface

An interface generally refers to a method or function for others to call, and it is an abstraction of behavior.

 

An interface can contain variables and methods. However, it should be noted that the variables in the interface will be implicitly designated as public static final variables (and can only be public static final variables, a compilation error will be reported with private modification), and methods will be implicitly designated as public abstract methods And it can only be a public abstract method (modified with other keywords, such as private, protected, static, final, etc., a compilation error will be reported), and all methods in the interface cannot have specific implementations, that is, the methods in the interface must be are abstract methods. From here, you can vaguely see the difference between an interface and an abstract class. An interface is an extremely abstract type, which is more "abstract" than an abstract class, and generally does not define variables in an interface.

 

The difference between abstract class and interface

1. Differences at the grammatical level

  1) Abstract classes can provide implementation details of member methods, while only public abstract methods exist in interfaces;

  2) Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type;

  3) An interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

  4) A class can only inherit one abstract class, but a class can implement multiple interfaces.

2. Differences at the design level

  1) An abstract class is an abstraction of a thing, that is, an abstraction of a class, while an interface is an abstraction of behavior. An abstract class abstracts the whole class, including attributes and behaviors, but an interface abstracts a part of the class (behavior).

2) The design level is different. As the parent class of many subclasses, the abstract class is a template design. The interface is a code of behavior, it is a radial design. The template design is to mention the common part, and the radial design is that the subclass must implement all the methods of the parent class

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326186893&siteId=291194637