Expand the class (bis:: abstract class) (2-5-2) Object-Oriented

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/gaolh89/article/details/80632220

In the previous chapter, we introduce the concept of usage, some details of the interface. If you are not very understanding of the interface, you can go to learn about. Click to Jump

This chapter, we introduce abstract classes, inner classes, enumeration.

A. An abstract class

Java, the class is an abstract concept interposed between the interfaces and classes: abstract class.

As the name implies, an abstract class is an abstract class. Abstraction is relatively Specifically, in general, have a specific class of objects correspond directly, without the abstract class, it represents the abstraction generally is the parent class of the concrete the upper comparison class example, a dog is a specific object, and the animal is an abstraction; cherry specific object, and the fruit is an abstraction; the positive direction is a specific object, and the graphics are some of abstraction let Tianfu Europe graphics processing. to illustrate the concept of abstract classes in Java.

1. Method abstract and abstract class

Before we introduced the graphical class Shape, which has a method draw (). Shape is actually an abstract concept, its draw () method does not really know how to achieve, only subclasses know that only subclasses know how method, generally defined as the abstract methods.

Abstract method is relative to the specific methods, specific methods and only abstract method declarations by the implementation code, does not implement interface methods in the previous section (non-Java 8 and the introduction of static default method) they are all abstract method.

Abstract methods and classes are suitable abstract keyword to declare the syntax is as follows:

public abstract class Shape {
    // ... 其他代码
    public abstract void draw();
}

Defines the abstract methods of the class must be declared abstract class, but an abstract class without abstract methods. Abstract and concrete classes, you can define the specific methods, instance variables, etc., the core difference between it and the concrete class is an abstract class can not be created objects (for example, can not use the new Shape ()), and specific class .

An abstract class can not create object To create an object, you must use its specific subclass. A class after inheriting an abstract class, all the abstract methods abstract class definition must be achieved unless it himself declared as an abstract class. Realize round classes code show as below:

public class Circle extends Shape {
    //...其他代码

    @Override
    public void draw() {
        // ....
    }
}

Implements a round draw () method is similar to the interface, though not using the new new abstract class, but it can declare variables abstract class, an abstract class object reference concrete subclasses, as follows:

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

Shape shape is an abstract class types of variables, the object references of concrete subclasses Circle, The draw () method invokes the draw Circle code.

2. Why do we need an abstract class

Abstract methods and classes appear to be superfluous, for the abstract method, does not know how to define an empty method body is not on line yet? Abstract class not to create an object that looks just adds an unnecessary restriction.

The introduction of abstract methods and classes, is a tool provided by Java syntax, for some classes and methods, to guide the user to use them correctly, reduce unnecessary use of abstract methods rather than empty method body, subclass knows it must implement the method, and impossible to ignore, if ignored Java compiler will prompt an error. use an abstract class, the class of user when creating the object, you must know that you want to use a specific sub-categories, and misuse impossible incomplete parent .

Whether it is to write a program, or usually do something else, everyone may make mistakes, reduce errors can not rely on the excellent quality of the people, but also need some mechanism so that an ordinary person can easily get things done right, it is difficult to get things wrong An abstract class is one such mechanism Java provides.

3. abstract classes and interfaces

Abstract classes and interfaces were similar, can not be used to create objects, interface methods are in fact abstract methods. If only the abstract class defines an abstract way, that abstract classes and interfaces more like a but abstract classes and radically different interface, the interface can not define instance variables , and abstract classes can, a class can implement multiple interfaces, but can only inherit a class.

Abstract classes and interfaces are not replace mating relationship, they are often used together, the ability to interface declaration, an abstract class provides default implementations, all or part of the method to achieve, an interface is often a corresponding abstract class.

For example, in Java class libraries, we are:

  • Collection interface and the corresponding abstract class AbstractCollection
  • List interface and the corresponding abstract class AbstractList
  • Map interface and the corresponding abstract class AbstractMap

Required for a particular implementation of the interface class, there are two options, one is to implement an interface to realize their full method, the other is an abstract class inheritance, and overridden method needed.

Advantage of inheritance is code reuse, only to rewrite the need, the need to write less code, easy to implement. However, if this particular class has a parent class, you can only choose to implement the interfaces.

We illustrate this further example of a mating relationship, or with the foregoing examples of add two sections, the upper section is introduced IAdd interface code is as follows:

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

We implement an abstract class AbstractAdder, 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 method implemented addAll, which is achieved by invoking the add method, the add method is an abstract method. Thus, the need to achieve IAdd interface class, it can be selected directly implemented IAdd interface, or from the class AbstractAdder inheritance, if inherited, only need to add a method to achieve it. Here, we let the original Base class inheritance AbstractAdder, the code looks like this:

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;    
        }
    }
}

4. Summary
In this section, we talk about abstract classes, with respect to the specific class, which is used to express abstract concepts, although syntactically, an abstract class is not necessary, but it can make procedures clearer, to reduce misuse, abstract class and interfaces are often cooperate, the ability to define an interface, abstract class provides default implementations, convenient subclass implements the interface.

Guess you like

Origin blog.csdn.net/gaolh89/article/details/80632220