Study Notes-Chapter VI Interface, Lambda and Internal Class "Java Core Volume I"

interface

1. Briefly, the interface ' if ' is a pure abstract class (stringent that must be added if the words, because the reference 4), the interface is not instance fields - there is no instance of an interface
exists between classes Common characteristics, extracting these common characteristics, the result is that
abstract classes cannot create objects, abstract classes cannot be instantiated, so abstract classes are used to be inherited by subclasses. The keywords final and abstract cannot be used at the same time. The two keywords are opposite.
Abstract classes have construction methods, which are used by subclasses.
Abstract methods must appear in abstract classes.

Use the keyword interface to replace class to declare the interface. The method in the interface is automatically public , and the keyword public is not required. When the interface is implemented, public must be added
(the fields in the interface are automatically declared as public static final )

An abstract class is a class that cannot create objects. Use the keyword abstract to mark a class as an abstract class
. 2. An abstract class can have abstract methods and non-abstract methods. The methods of abstract classes have no content. The abstract methods will end as long as you write (). eg

public abstract void func();

3. If the class has an abstract method, then it must be an abstract class. 4.
Java can only inherit single inheritance (extends is inheritance), multiple inheritance will lead to the problem of "
fatal square" , fatal square:

if class A uses a method fun1, B Class inherits A, rewrite fun1 to get fun1', C class inherits A, rewrite fun1 to get fun1'', D class has multiple inheritance, while inheriting B, C, and D without rewriting methods, then call D class fun1 Yes, I don’t know if fun1 in B or fun1 in C is called''

Java cleverly introduced interfaces to achieve the effect of multiple inheritance

public class Dog extends Animal implements Pet,Saveable,(...)

There can only be one extends, multiple implements, different inheritance classes can implement the same interface, extends is inheritance, and implements implements the interface (note that the syntax has's').
Then, by implementing the interface, the original expectation is achieved The effect of multiple inheritance!

Guess you like

Origin blog.csdn.net/weixin_39666736/article/details/105848631