Java Basics Summary ① abstract ② Interface ③ Design the principles of abstract classes and interfaces ④ interface and abstract class differences
I. Abstract abstraction
Function: Cannot produce object, act as parent class, force subclass to implement overriding method correctly
The only change that can be compared to a class is that it cannot produce objects, others include constructs, attributes, and so on.
Any class that has an abstract method becomes an abstract class.
Abstract method public abstract A ();
The ① method is abstract, and the class is abstract;
② subclasses must override abstract methods, unless subclasses are also abstract classes
Abstract classes can have no abstract methods, but they are not generally designed
Second, interface interface
Interfaces are also a reference data type for Java (Java Reference data types include: classes, arrays, interfaces)
Role: Allow classes that do not inherit relationships to share behavior
Naming is usually followed by a able, which means the ability to have what.
① property: Can only be public, static, constant property
It's not written, it's the default.
int d = 10;//public static final int d = 10;
② method: can only be public abstract method (JDK1.8 previously, 1.1-1.7)
Default: Public abstract
Class implementation interface: Implements interface, there is no way to implement the interface, can only be abstract class
The reference to an interface can point to any one of the implementation class objects and see only the behavior or properties defined in the interface
public void Service (serviceable Ser) {
Ser.changoil ();
Ser.turnupenginee ();
}
An interface can inherit an interface and can inherit multiple, because the interface is not a class and is also a reference to that data type
Public interface C extends Interb, intera{
}
Iii. principles of design and design interfaces
Innate behavior is inherent in the parent class method (parent Class)
Secondary methods are written in the interface Optional (interface)
Design interface principle: Minimize the interface principle, can be a variety of combinations
Using interface principles: using interfaces to implement multiple inheritance, using interfaces to add functionality to external classes
Iv. differences between interfaces and abstract classes ① syntax ② design
① syntax
Interface
Definition: Keyword: interface. Content: ① public static constant property ② public abstract method
Use: Class implements interface implement multiple implementations. Interface inheritance interface, multiple inheritance
Abstract class
Definition: Keyword: abstract class. Content: Any property
Use: class inherits extends abstract class, single inheritance
② Design
Subordinate behavior written in the interface, as far as possible to design a small interface
Innate behavior written in abstract class inside
2.35 Java Basics Summary ① abstract ② Interface ③ design abstract class and interface principles ④ Interface and abstract class differences