[Java keywords] abstract abstraction

Abstract classes exist for inheritance (rewriting). If an abstract class is defined but not inherited, it is useless. Of course, after inheritance, the class must implement all unimplemented methods in the parent class, otherwise it is not allowed (unless it is an abstract class inherited by an abstract class, which may not be implemented at this time).

abstract class

Classes with abstract methods must be abstract classes. Abstract classes, like ordinary classes, can have no abstract methods, and can have member variables and ordinary member methods.
Because the abstract class contains methods that have no concrete implementation, you cannot use the abstract class to create instance objects!
There can be static methods, which can be called by the instantiated object of the subclass after being inherited by the subclass ; it can also be called directly by the class name of the abstract class and subclass, just like other static methods. The same static variables can also be accessed using the same method.
Of course, there may be a class that is only decorated with abstract, but there is no abstract method. This is also an abstract class and cannot be instantiated. If there is no abstract method, why should it be designed as an abstract class?

AbstractStringBuilder.class(jdk1.8)

//有抽象方法的类,必须是抽象类。public abstract String toString()
abstract class AbstractStringBuilder implements Appendable, CharSequence {
    char[] value;
    int count;
    AbstractStringBuilder() {
    }
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }
    //一个类,只用abstract修饰,但是却没有抽象方法,这也属于抽象类,是不可以实例化的
	public AbstractStringBuilder append(char[] str) {
	        int len = str.length;
	        ensureCapacityInternal(count + len);
	        System.arraycopy(str, 0, value, count, len);
	        count += len;
	        return this;
	    }
	@Override
    public AbstractStringBuilder append(char c) {
        ensureCapacityInternal(count + 1);
        value[count++] = c;
        return this;
    }
	@Override
	public abstract String toString();//因为抽象类中含有无具体实现的方法,所以不能用抽象类创建实例对象!
}

abstract method

There can be no static abstract methods in Java abstract classes.
Abstract classes cannot be instantiated, that is, they cannot be allocated memory; and statically modified methods do not allocate memory before the class is instantiated, so a contradiction arises: abstract classes cannot be allocated memory, and static methods must Memory is allocated. So there can be no static abstract methods in the abstract class.
In addition, the purpose of defining an abstract method is to rewrite this method, but if it is defined as a static method, it cannot be rewritten.

Abstract class and interface comparison

Grammatical difference

The interface can only have public abstract methods, the abstract class has more method types, and can have ordinary methods;
the member variables in the interface can only be of public static final type, the abstract class has more variable types, and can have ordinary variables; the
interface It cannot contain static code blocks and static methods, and abstract classes can have static code blocks and static methods;
a class can only inherit an abstract class, but a class can implement multiple interfaces.

Design differences

  1. An abstract class is an abstraction of things (class abstraction), and an interface is an abstraction of behavior.
    For example, Bird is an abstract class, and various birds implement it. The definition of an interface is fly, and various birds can implement this interface. In layman's terms: inheritance is a "is it right" relationship, while interface implementation is a "is there" relationship. Birds inherit Bird, and it is destined for its type, and as to whether to implement the fly interface, just think it has this feature (behavior).

  2. The abstract class is a template design, the interface is a behavioral specification, and a radial design.
    For example, for the same method, after the abstract class template design, when modifying the method, only need to modify in the template, all subclasses can be updated; after the interface radial design, when modifying the method, you need to All changes in the implementation class can be updated and are independent of each other.

The connection between the two

  1. If the interface inherits, the interface must be inherited. Not possible. Cannot inherit abstract class

  2. Abstract classes can implement interfaces, can inherit abstract classes, and cannot inherit interfaces

Reference: https://www.i3geek.com/archives/1230

Published 99 original articles · Like 106 · Visit 270,000+

Guess you like

Origin blog.csdn.net/lglglgl/article/details/105042886