Thinking logic of computer programs (20), thinking Logic

Source: Internet
Author: User

Thinking logic of computer programs (20), thinking Logic

Basic Concepts

The previous section mentioned a concept. What is an abstract class? As the name implies, an abstract class is an abstract class. abstract is relative to a specific class. Generally, a specific class has a direct corresponding object, but an abstract class does not. It expresses abstract concepts, it is generally the parent class at the upper layer of a specific class.

For example, dogs are specific objects, animals are abstract objects, cherries are specific objects, fruits are abstract objects, squares are specific objects, and graphics are abstract objects. The following examples illustrate the abstract classes in Java.

Abstract methods and abstract classes

We have introduced Shape in graphics before. It has a method draw (). Shape is actually an abstract concept. Its draw method does not actually know how to implement it. Only Child classes know it. This method is generally defined as an abstract method.

Abstract methods are relative to specific methods. Specific methods have implementation code, while abstract methods only have declarations and are not implemented. Methods in the interfaces described in the previous section are abstract methods.

Abstract methods and abstract classes are declared using the abstract keyword. The syntax is as follows:

Public abstract class Shape {//... other code public abstract void draw ();}

Classes defining abstract methods must be declared as abstract classes. However, abstract classes can have no abstract methods. Abstract classes can define specific methods, instance variables, and so on, just like a specific class. They are the core of a specific class. abstract classes cannot create objects (for example, they cannot use new Shape ()), the specific class can.

An abstract class cannot create an object. To create an object, you must use its specific subclass. After a class inherits the abstract class, it must implement all the abstract methods defined in the abstract class, unless it is declared as an abstract class. The implementation code of the circle class is as follows:

Public class Circle extends Shape {//... other code @ Override public void draw (){//....}}

Circle implements the draw () method. Similar to the interface, although the abstract class cannot use new, it can declare the variable of the abstract class and reference the object of the specific subclass of the abstract class, as shown below:

Shape shape = new Circle();shape.draw();

Shape is a variable of the Shape type of the abstract class. It references the objects of the specific subclass Circle. Calling the draw method will call the draw code of the Circle.

Why abstract classes?

Abstract methods and abstract classes seem redundant. For abstract methods, you do not know how to implement them. It is not enough to define an empty method body. abstract classes do not allow you to create objects, it seems that an unnecessary restriction is added.

Introducing abstract methods and abstract classes is a syntax tool provided by Java. For some classes and methods, users are guided to use them correctly to reduce misuse.

When an abstract method is used, instead of an empty method body, the subclass knows that it must implement the method, rather than ignoring it.

When using an abstract class, the user of the class knows that he must use a specific subclass when creating an object, and cannot misuse an incomplete parent class.

When writing a program or doing anything at ordinary times, everyone may make mistakes. Reducing errors cannot only rely on the excellent quality of people, but also requires some mechanisms, it makes it easy for an ordinary person to do things right, but it is difficult to do things wrong. Abstract classes are a mechanism provided by Java.

Abstract classes and interfaces

Abstract classes are similar to interfaces and cannot be used to create objects. Methods in interfaces are abstract methods. If only abstract methods are defined in an abstract class, the abstract class and interface are more similar. Abstract classes and interfaces are fundamentally different. A class can implement multiple interfaces, but only one class can be inherited.

Abstract classes and interfaces are in a cooperative rather than substitution relationship. They are often used together, and the interface declaration capability. abstract classes provide default implementations to implement all or some methods. 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 a specific class that needs to implement an interface, there are two options: one is to implement the interface and implement all the methods by yourself, the other is to inherit the abstract class, and then rewrite the method as needed.

The benefit of inheritance is to reuse the code. You only need to rewrite the code you need. You need to write less code and implement it easily. However, if the specific class already has a parent class, you can only choose to implement the interface.

Let's take an example to further illustrate this cooperative relationship. We still use the add example in the previous two sections. The IAdd interface is introduced in the previous section. The Code is as follows:

public interface IAdd {    void add(int number);    void addAll(int[] numbers);}

We implement an abstract class AbstractAdder. The Code is as follows:

public abstract class AbstractAdder implements IAdd {    @Override    public void addAll(int[] numbers) {        for(int num : numbers){            add(num);        }    }}

This abstract class provides the implementation of the addAll method, which is implemented by calling the add method, and the add method is an abstract method.

In this way, for the class that needs to implement the IAdd interface, it can choose to directly implement the IAdd interface, or inherit from the AbstractAdder class. If it inherits, you only need to implement the add method. Here, we let the original Base class inherit AbstractAdder. The Code is as follows:

public class Base extends AbstractAdder {    private static final int MAX_NUM = 1000;    private int[] arr = new 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. Compared with specific classes, they are used to express abstract concepts. Although the abstract classes are not necessary in syntax, they can make the program clearer and reduce misuse, abstract classes and interfaces often work together to define interfaces. abstract classes provide default implementations to facilitate subclass interfaces.

In the current class description, each class is independent and corresponds to a Java source code file. However, in Java, a class can also be placed inside another class, called an internal class, why should a class be placed inside another class?

----------------

For more information, see the latest article. Please pay attention to the Public Account "lauma says programming" (scan the QR code below), from entry to advanced, ma and you explore the essence of Java programming and computer technology. Write with your heart, original articles, and retain all copyrights.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.