Earlier, we introduced package, inheritance, rewriting and other methods of object-oriented programming. This time, let's take a look at abstract classes and interfaces.
catalogue
Abstract classes cannot be instantiated:
Abstract classes can also contain the same members and methods as ordinary classes
Abstract classes and abstract methods cannot be modified by Final
The abstract methods in the interface are public abstract by default
The interface cannot be instantiated through the new keyword
Abstract methods in interfaces
The method in the interface is modified by public static final by default
When a class implements an interface and rewrites this method, it must be preceded by public
1. Abstract class
For example, we define a parent class, but this parent class does not play a certain role in the process of program operation. The main work is completed by the methods of its subclasses. For methods without actual work, we can design it as an abstract method
method), and the class containing abstract methods is called abstract class
Let's take a concrete look at the implementation process of the abstract class through the code
abstract class Shape { public abstract void draw();//Abstract methods, without {}, cannot implement concrete code } //The class containing abstract methods is modified with abstract to indicate that it is an abstract class
Abstract classes cannot be instantiated:
Abstract classes can also contain the same members and methods as ordinary classes
abstract class Shape { public int a; public void func(){ System.out.println("General test method"); } }
Because it cannot be instantiated, this abstract class can only be inherited, and its greatest function is to be inherited
abstract class Shape { public abstract void draw();//Abstract method } //After inheriting the abstract class, override the methods in the abstract class class Rect extends Shape{ @Override public void draw() { System.out.println("🧡"); } }
If an ordinary class inherits an abstract class, all abstract methods in the abstract class need to be overridden in the ordinary class
If an abstract class a inherits an abstract class B, the abstract class A may not implement the abstract methods of the abstract parent class B
For example:
abstract class Shape { public void func(){ System.out.println("General test method"); } public abstract void draw();//Abstract method } abstract class A extends Shape{ public abstract void funcA(); }
Combined with the above point, when class A is inherited by an ordinary class again, the abstract methods in the two abstract classes a and B must be overridden
abstract class Shape { public abstract void draw();//Abstract method } abstract class A extends Shape{ public abstract void funcA(); } class B extends A{ @Override public void funcA() { } @Override public void draw() { } }
Abstract classes and abstract methods cannot be modified by Final
The role of abstract classes: the greatest significance of an abstract class is to be inherited. An abstract class itself cannot be instantiated. If you want to use it, you can only create a subclass of the abstract class. Then let the subclass rewrite the abstract methods in the abstract class. Some students may say that ordinary classes can also be inherited and ordinary methods can also be rewritten. Why do you have to use abstract classes and abstract methods? That's true. But using an abstract class is equivalent to an extra check of the compiler. The scenario of using an abstract class is like the above code. The actual work should not be completed by the parent class, but by the child class. At this time, if the parent class is accidentally misused, the ordinary class compiler will not report an error. However, if the parent class is an abstract class, an error will be prompted when instantiating, Let's find the problem as soon as possible.
2. Interface
An interface is a further step of an abstract class. An abstract class can also contain non abstract methods and fields. While the methods contained in an interface are abstract methods, and fields can only contain static constants
Interface implementation example:
Use interface to implement, i.e. interface IA {}
Code implementation:
interface IShape{ public abstract void draw();//Abstract method }
The method in the interface cannot have a specific implementation. If it is to be implemented, it must be preceded by default
interface IShape{ public abstract void draw();//Abstract method //Method cannot be implemented. If you want to implement it, you must add default default public void func(){ System.out.println("fassa"); } default public void func2(){ System.out.println("fassa"); } }
You can have static methods in the interface, or you can not add public, because the methods in the interface are public by default
interface IShape{ public abstract void draw();//Abstract method //You can have static methods without public, because the methods in the interface are public by default public static void funStatic(){ System.out.println("fafa"); } }
The abstract methods in the interface are public abstract by default
The interface cannot be instantiated through the new keyword
The relationship between classes and interfaces is implemented through implements, but when a class implements an interface, it must be overridden
Abstract methods in interfaces
interface IShape{ public abstract void draw();//Abstract method default public void func(){ System.out.println("fassa"); } } class Rect implements IShape{ @Override public void draw() { System.out.println("♦"); } @Override public void func(){ System.out.println("Override the default method in the interface"); } }
The method in the interface is modified by public static final by default
interface IShape { void draw(); public static final int num = 10; }
When a class implements an interface and rewrites this method, it must be preceded by public
interface IShape{ public abstract void draw();//Abstract method default public void func(){ System.out.println("fassa"); } } class Rect implements IShape{ @Override public void draw() { System.out.println("♦"); } @Override public void func(){ System.out.println("Override the default method in the interface"); }
A class can inherit an abstract class or ordinary class through the keyword extends, but can only inherit one class. At the same time, you can also implement multiple interfaces through implements, which are separated by commas.
interface IA { //Must initialize int A = 10; void funcA();//The default is public abstract } interface IB { void funcB(); } class BClass { } //A class can inherit an abstract class or ordinary class through the keyword extends, but can only inherit one class. At the same time, it can also inherit through implements //Implement multiple interfaces, which are separated by commas class AClass extends BClass implements IA ,IB{ //The overriding condition is that the permission of the method in the subclass must be greater than or equal to the permission of the parent class, but there is no modification. The default is package access permission, which is less than public. Therefore, public modification should be used here public void funcA(){ System.out.println("A::funcA()"); System.out.println(A); } @Override public void funcB() { System.out.println("A::funcA()"); } }
Interfaces and interfaces can use extensions to operate the relationship between them. At this time, it means expansion. An interface B extends the functions of another interface C through extensions. At this time, when a class D implements this interface B through implements, the rewritten method is not only B's method, but also the functions extended from the C interface.
interface IA1{ void funcA(); } //Interfaces and interfaces can use extensions to operate their relationships. At this time, it means expansion, //One interface B extends the functions of another interface C through extensions. At this time, when a class D implements this interface B through implements. //At this time, the rewritten method is not only the method of B, but also the function (method) extended from the C interface interface IB1 extends IA1{ void funB(); } class C implements IB1{ @Override public void funB() { System.out.println("fassa"); } @Override public void funcA() { System.out.println("erwtyuu"); } }