Basic concepts
The last section mentions a concept, abstract class, what is abstract class? As the name implies, abstract class is abstract class, abstract is relative to the specific, in general, the specific class has a direct corresponding to the object, and the abstract class does not, it expresses the abstract concept , is generally the comparison of the class of the parent class.
For example, a dog is a concrete object, while an animal is an abstract concept, a cherry is a concrete object, whereas a fruit is an abstract concept, a square is a concrete object, and a graph is an abstract concept. Let's take a few examples to illustrate the abstract classes in Java.
Abstract methods and abstract classes
Before we introduced the Graphic class shape, it has a method draw (), shape is actually an abstract concept, its draw method actually does not know how to implement, only the subclass knows. This method, which only subclasses know how to implement, is generally defined as an abstract method.
Abstract methods are relative to the specific method, the specific method has implementation code, and the abstract method only declaration, no implementation, the interface described in the previous section is an abstract method.
Both abstract methods and abstract classes are declared using the Abstrac T keyword, which isshown in the following syntax:
Class Shape { //void Draw ();}
A class that defines an abstract method must be declared as an abstract class, but an abstract class can have no abstract method. Abstract classes, like concrete classes, can define specific methods, instance variables, and so on, and the core difference between them and concrete classes is that abstract classes cannot create objects (for example, you cannot use new Shape ()), and a specific class can.
An abstract class cannot create an object, and it must use its specific subclasses to create an object. after a class inherits an abstract class, it must implement all the abstract methods defined in the abstract class, unless it declares itself as an abstract class. The implementation code for the Round class is as follows:
Extends Shape { //... Other code void// .... }}
The circle implements the draw () method. Like interfaces, abstract classes cannot use new, but can declare variables of an abstract class, referencing an object of the abstract class-specific subclass, as follows:
New Circle (); Shape.draw ();
Shape is a variable of the abstract class shape type, referencing the object of the specific subclass circle, calling the draw method to invoke the draw code of circle.
Why do you need abstract classes?
Abstract methods and abstract classes seem redundant, for abstract methods, do not know how to implement, define an empty method body is not OK, and the abstract class does not let the creation of objects, it seems to only add an unnecessary limit.
The introduction of abstract methods and abstract classes is a syntax tool provided by Java, for classes and methods that guide users to use them correctly and to reduce misuse.
Using an abstract method rather than an empty method body, the subclass knows that he must implement the method, and that it is not possible to ignore it.
Using abstract classes, when the consumer of a class creates an object, it is known that he must use a specific subclass, and that it is not possible to misuse the incomplete parent class.
Whether it is writing a program, or doing anything else at ordinary times, everyone can make mistakes, reduce errors can not only rely on the excellent quality of people, but also need some mechanism, making it easy for an ordinary person to do things right, and difficult to do things wrong. An abstract class is one such mechanism provided by Java.
Abstract classes and Interfaces
Abstract classes and interfaces are similar in that they cannot be used to create objects, and the methods in an interface are actually abstract methods. If only abstract methods are defined in an abstract class, then the abstract class and interface are more like. But abstract classes and interfaces are fundamentally different, and a class can implement multiple interfaces, but only one class is inherited.
Abstract classes and interfaces are mates rather than alternative relationships, they are often used together, interface declaration capabilities, abstract classes provide default implementations, implement all or part of a method, and an interface often has a corresponding abstract class.
For example, in the Java class Library, there are:
- Collection interface and corresponding Abstractcollection abstract class
- List interface and corresponding Abstractlist abstract class
- Map interface and corresponding ABSTRACTMAP abstract class
For the specific class that needs to implement the interface, there are two choices, one to implement the interface, to implement all of the methods, and the other to inherit the abstract class, and then override the method as needed.
The benefit of inheritance is the reuse of code, only the need to rewrite, need to write less code, easy to implement. However, if the specific class already has a parent class, then you can only choose to implement the interface.
We use an example to further illustrate this tie-up, or with the example of add in the previous two sections, the Iadd interface is introduced in the previous section, and the code is as follows:
Interface Iadd { void Add (intvoid AddAll (int[] numbers);}
We implement an abstract class Abstractadder with the following code:
Implements Iadd { @Override void AddAll (int for(int num:numbers) {add (num);}}}
This abstract class provides an implementation of the AddAll method, which is implemented by invoking the Add method, and the Add method is an abstract method.
In this way, for a class that needs to implement the Iadd interface, it can choose to implement the Iadd interface directly, or inherit from the Abstractadder class, and if it inherits, simply implement the Add method. Here, we let the original base class inherit Abstractadder, and the code looks like this:
PublicClass BaseExtends Abstractadder {private static final int max_num = 1000 ; private int[] arr = new Span style= "COLOR: #0000ff" >int[max_num]; private int count; @ Override public void Add ( Int number) {if (Count< Max_num) {arr[count++] = number;}}
Summary
In this section, we talk about abstract classes, relative to specific classes, which are used to express abstract concepts, although syntactically, abstract classes are not required, but it can make the program clearer, reduce misuse, abstract classes and interfaces often cooperate with each other, interface definition capabilities, and abstract classes provide a default implementation, convenient sub-class implementation interface.
In the current description of the class, each class is independent and corresponds to a Java source code file, but in Java, a class can also be placed inside another class, called an inner class , why do you want to put a class inside another class?
----------------
Thinking Logic of computer programs (20)-Why abstract classes? Go