Java Abstract Class
Our parent class has set some methods. The main purpose of setting them is to let the subclass inherit the parent class to override those methods to show different results. In other words, we don't care about the specific implementation of the parent class method. It will be overridden by the subclass method anyway. Then we can make the parent class more abstract, so abstract that it only has the method declaration and no method body. We call this method an abstract method, and the class containing the abstract method is called an abstract class.
Characteristics of abstract classes
As long as an abstract class contains one or more abstract methods, it is an abstract class. However, if there is no abstract method, then this abstract class has no meaning. An abstract method is a method with the following format
[public] abstract return type method name (parameter list);
Abstract methods have no method body, so after the method name is written; will be added to indicate the end of the method declaration, and the abstract method method body Modified with abstract, it means that this is an abstract method. The access modifier can only be public or protected, or the default access permission. Private cannot be used because it cannot be inherited at all.
Similarly, the format of an abstract class is as follows:
[public] abstract class 类名{ [public] abstract 返回类型 方法名(参数列表); }
The abstract class is modified by adding abstract in front, indicating that it is an abstract class. The access modifier can only be public or protected, or the default access permission. The reason why private cannot be used is the same as above.
The meaning of abstract class creation is to separate the declaration of methods from the implementation of methods, thereby achieving polymorphism. Then it has the following characteristics:
Abstract classes cannot be instantiated, which means that an object of an abstract class cannot be created directly, but abstract classes can have constructors. If it is a parameterized constructor, the subclass must Go to show call it.
Abstract classes are meant to be inherited, and methods need to be overridden. If the subclass inherits the abstract parent class, it needs to override the abstract method of the parent class. If it is not overridden, the subclass must also be defined as abstract class.
abstract cannot be used together with private static, final modifiers to modify methods.
Here we explain the third point, abstract cannot be used with private, because the methods and classes modified by private cannot be accessed outside the class. There is no possibility of inheritance. Abstract cannot be used with static because the function of abstract is to achieve polymorphism, and achieving polymorphism relies on inheritance and overwriting. Although statically modified methods can be inherited by subclasses, when we modify the inherited method, this cannot be counted as overwriting. Instead, the method of the parent class is hidden, and only the parent class name and method name can be used. The form shows that calling it does not achieve polymorphism. From another perspective, static methods are determined during compilation, and late binding cannot be achieved, so there is no possibility of determining method calls at runtime. Therefore, the static modified method can be inherited, but it cannot achieve polymorphism, and naturally it cannot be used with abstract.
The reason why abstract cannot be used with final is the same as above. The method modified by final cannot be inherited, and naturally it is not polymorphic, so abstract cannot be used with final.
Example of abstract class
We will continue to modify and abstract the polymorphic example above. We change the Animal code to look like this.
public abstract class Animal { abstract void run(); }
The code of our Dog and Cat classes does not need to be changed.
public class Dog extends Animal{ @Override public void run() { System.out.println("狗在奔跑"); } }public class Cat extends Animal{ @Override public void run() { System.out.println("猫在奔跑"); } }
The codes of other Print classes and Test classes also remain unchanged. The code is as follows:
public class Print { public void print(Animal animal) { animal.run(); } } public class Test { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); new Print().print(dog); new Print().print(cat); } }
We can see that there are basically no changes in other places between the abstract class and the previous ordinary class, except that the method is made abstract , the meaning is more clear.
Abstract summary
It is very meaningful to create abstract classes and abstract methods. It allows us to make the meaning of the class clearer when designing the class. Through abstract ideas, the class becomes more versatile and declares a series of methods. to tell users how to use it.
Java Interface
The interface can be seen as a protocol and a requirement. We inherit it to know what we can do, and how to do it depends on us. For example, KFC is an interface. As soon as we look at it, we know that there are burgers, fried chicken, and cola, but the specific taste and service will look different according to different stores. This is the meaning of the interface. So we can also see here that the methods in the interface should also be abstract.
Characteristics of interfaces
The way to write interfaces in Java is as follows:
[public] interface InterfaceName { 成员变量 方法声明 }
Interfaces are different from classes. They are not modified with classes. They use specific interface keywords. The access modifiers are consistent with class and can be public or default. . There are only two types of content: abstract methods and member variables. Member variables will be added with public static final by default, which means that member variables are classified as owned and have the greatest access rights, but they cannot be inherited or modified. This also shows that the interface is agreed in this way because it cannot be instantiated. The member variables of the interface are not allowed to be empty and must be assigned to them when they are defined. Interface methods default to abstract methods. Public abstract is added by default. Like abstract methods, only method declarations can be written.
Unlike us using extends to inherit a class, we use implements to represent the implementation of this interface.
class ClassName implements Interface1{ }
As a special existence, the interface has some unique features.
一个类是可以实现多个接口的,这在一定程度上实现了Java的多继承。
接口是不能被实例化,不同于抽象类,接口的内部只能由成员变量和抽象方法,是不可以存在静态变量块,以及构造器的。
我们是可以声明一个接口类型的变量,但是只能引用一个实现了这个接口的类。
同抽象方法,实现了接口的类必须实现接口的所有方法,否则就会变成抽象类。
接口举例
看过抽象的例子,我们可能想,我们把Animal从抽象换成接口,不就实现了一个接口的例子嘛,其他地方基本也不用去改动。但这显然是错的。我们并不能去说Animal是一个接口,我们上面说了,接口是一种协议,规定我们能做什么,而不是一个事物的抽象。从这里我们也能看出接口和抽象的不同,抽象更多的是一种重构而产生的东西,我们先有dog,cat类,然后才会把他们共性的东西提取出来,放到一个更通用,更抽象的父类Animal中,而我们发现Animal不需要管run方法是怎么实现的,所以我们将run方法设定为抽象的方法,从而将Animal类设为抽象类,等待去=继承者来实现run。这是一种从下而上的设计思想。但是接口不是,接口一开始就是设定好的,我们根据设定好的接口从上往下去写。接口先规定好了有什么方法,然后我们再去具体实现他。这是一种从上而下的设计思想。
所以,我们不能将Animal设为接口,但是我们可以将Print设为接口,他规定我们有一个print()方法,代码如下:
public interface Print { void print(Object obj); }
那我们就可以写一个新的类去实现这个Print接口。代码如下:
public class SimplePrint implements Print{ @Override public void print(Object obj) { ((Animal)obj).run(); } }
除了Test类以外,其他地方都不需要改变。我们Test类代码如下:
public class Test { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); Print print = new SimplePrint(); print.print(dog); print.print(cat); } }
接口总结
接口和抽象虽然都是通过抽象的方法来提供我们实现多态的方式,但是他们却是两个不同的设计思想。这里关于接口的讲解比较简单,关于接口自身的继承,接口内部包含其他接口,以及利用接口来实现回调等等留在以后的文章专门来说。这里主要是通过对比来了解抽象和接口。