This article has included “Java often meet questions” : gitee.com/mydb/interv…
Java is an object-oriented programming language. Object-oriented programming languages have four characteristics: abstraction, encapsulation, inheritance and polymorphism. The interface and abstract class introduced in this paper are the concrete implementation of “abstraction” in object-oriented programming, that is to say, interface and abstract class are used to define the common behavior of entity class, they are higher level abstraction of entity class (object).
Note: This article is based on JDK 8.
interface
An interface is an abstract type in the Java language that defines the common behavior of an object. The static and default methods can be created in JDK 8, and the default methods can be implemented as shown in the following code:
public interface Interface_1 {
int count = 1;
void sayHi(a);
/ / the default method
default void print(a) {
System.out.println("Do print method.");
}
/ / static method
static void smethod(a) {
System.out.println("Do static method."); }}Copy the code
Next, create a class that implements the above interface:
public class InterfaceImpl_1 implements Interface_1 {
@Override
public void sayHi(a) {
System.out.println("Hi,I am InterfaceImpl 1.");
}
public static void main(String[] args) {
InterfaceImpl_1 inter = new InterfaceImpl_1();
inter.sayHi();
// Call the static method on the interface
InterfaceExample.smethod();
// Call the constant count in the interfaceSystem.out.println(InterfaceExample.count); }}Copy the code
The execution results of the above procedures are as follows:Through the above code and execution results, we can draw the following conclusions:
- In JDK 8, interfaces can define static and default methods, and both methods can contain specific code implementations.
- Implements the interface using the implements keyword.
- The interface cannot be instantiated directly.
- Variables defined in the interface default to the public static final type.
- Subclasses may not override the static and default methods of the interface. If they do not override the static and default methods of the interface, they default to calling the method implementation of the interface.
An abstract class
Abstract classes are similar to interfaces in that they define the common behavior of objects and cannot be instantiated directly. The implementation keyword of an abstract class is Abstract class, and subclasses inherit their parent class with the extends keyword. The use of abstract classes is as follows:
public abstract class AbstractExample {
// Define common variables
int count = 2;
// Define private variables
private static int total = 10;
// Define abstract methods
public abstract void methodA(a);
// Define ordinary methods
public void methodB(a) {
System.out.println("Hi,methodB."); }}Copy the code
Next use a normal class to inherit the above abstract class:
public class AbstractSon extends AbstractExample {
@Override
public void methodA(a) {
System.out.println("Hi,method A.");
}
public static void main(String[] args) {
AbstractSon abs = new AbstractSon();
// Reassign variables in the abstract class
abs.count = Awesome!;
System.out.println(abs.count);
// Abstract methods in abstract classes
abs.methodA();
// Ordinary methods in abstract classesabs.methodB(); }}Copy the code
The execution results of the above procedures are as follows:The following conclusions can be drawn from the above code and execution results:
- Abstract classes are declared using the abstract keyword.
- Abstract classes can contain both ordinary and abstract methods, and abstract methods cannot have concrete code implementations.
- Abstract classes require inheritance using the extends keyword.
- Abstract classes cannot be instantiated directly.
- Property controllers in abstract classes are unrestricted and can define properties of private type.
Interfaces differ from abstract classes in the following seven areas.
Difference 1: Different keywords are defined
Interfaces are defined using the keyword interface. Abstract classes are defined using the keyword abstract.
Difference 2: Different inheritance or implementation keywords
An interface defines its implementation using the implements keyword. Abstract classes implement inheritance using the extends keyword.
Difference 3: The number of subclass extensions is different
An interface can have multiple implementation classes, as shown in the following figure:An error is reported when an abstract class subclass inherits only one abstract class, as shown in the following figure:In the Java language, a class can only inherit from one parent (single inheritance), but can implement multiple interfaces.
Difference 4: Attribute access control characters are different
The access control for attributes in the interface can only be public, as shown in the following figure:
Properties in interfaces are public static final by default.
The attribute access control characters in the abstract class are unlimited and can be arbitrary, as shown in the following figure:
Difference 5: Method control characters are different
The default control for methods in the interface is public and cannot be defined as other controls, as shown below:Method controls in abstract classes are unrestricted, where abstract methods cannot be decorated with private, as shown in the following code:
Difference 6: Different method implementations
Static and default methods must be implemented after JDK 8, as shown in the following code:As you can see from the above results, static or default methods report an error if they have no method implementation, while normal methods report an error if they have one.
Ordinary methods in an abstract class can have methods implemented, but abstract methods cannot have methods implemented, as shown in the following code:As you can see from the above results, ordinary methods in an abstract class will report an error if they have no method implementation, while abstract methods will report an error if they have method implementation.
Difference 7: Static code blocks are used differently
Static code blocks cannot be used in interfaces, as shown below:Static code blocks can be used in abstract classes, like this:
conclusion
Interfaces and abstract classes are used to define the common behavior of objects, but they differ in seven ways:
- The keywords defined are different.
- Subclass inheritance or implementation keywords are different.
- Type extension is different: abstract classes are single-inherited, whereas interfaces are multi-inherited.
- Method access control: The abstract class is unrestricted, but the abstract method in the abstract class cannot be modified by private; Interfaces have limitations. Interfaces default to public control characters.
- Attribute method controls: Abstract classes are unrestricted, while interfaces are restricted. Interfaces default to public controls.
- Methods are implemented differently: ordinary methods in abstract classes must be implemented, while abstract methods must not be implemented. Normal methods cannot be implemented in interfaces, but static and defualt methods in JDK 8 must.
- Static code blocks are used differently: Abstract classes can have static code blocks, whereas interfaces cannot.
What else do you know about interfaces and abstract classes? Welcome to the comments section.
Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.
Public account: Java Chinese Community