java Foundation_ Abstract classes and interfaces
1 abstract classes and abstract methods
Abstract method: a method with only function declaration and no function body implementation.
Abstract class: a class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class must be limited to an abstract class.
// Abstract method definition format public abstract Return value type method name(parameter); // Abstract class definition format public abstract class Class name{ // ... }
If class A inherits an abstract class, class A must provide method definitions for all abstract methods in the abstract class, that is, override the abstract methods. If not overridden, class A must also be declared abstract. The purpose of creating abstract classes is to manipulate A series of classes through this common interface.
Note: abstract classes cannot create objects directly. They can only be inherited by subclasses to create subclass objects.
2 interface
Interface: it is a declaration of a series of methods and a collection of method features. An interface has only method features and no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions). That is, an interface is a special abstract class, which is composed of global constants and public abstract methods.
The interface has no specific implementation, and multiple inheritance can be implemented. When combining a concrete class with multiple interfaces, the concrete class must be placed in front, followed by the interface.
When a class wants to implement an interface, the overridden interface method must be defined as public.
Any fields put into the interface are automatically static and final
interface Test{ // public,static and final int a = 1; // public and abstract void display(); } class HelloWrold implements Test{ @Override public void display(){ System.out.println("Hello world!"); } }
3 interface and design mode
3.1 strategy mode
Policy pattern: create a method that can have different behavior according to different parameter objects passed. Policy is the parameter object passed in, which contains the code to be executed. The following code is the implementation of a policy pattern.
//: interfaces/classprocessor/Apply.java package interfaces.classprocessor; import java.util.*; import static net.mindview.util.Print.*; class Processor { public String name() { return getClass().getSimpleName(); } Object process(Object input) { return input; } } class Upcase extends Processor { String process(Object input) { // Covariant return return ((String)input).toUpperCase(); } } class Downcase extends Processor { String process(Object input) { return ((String)input).toLowerCase(); } } class Splitter extends Processor { String process(Object input) { // The split() argument divides a String into pieces: return Arrays.toString(((String)input).split(" ")); } } public class Apply { public static void process(Processor p, Object s) { print("Using Processor " + p.name()); print(p.process(s)); } public static String s ="Disagreement with beliefs is by definition incorrect"; public static void main(String[] args) { process(new Upcase(), s); process(new Downcase(), s); process(new Splitter(), s); } } /* Output: Using Processor Upcase DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT Using Processor Downcase disagreement with beliefs is by definition incorrect Using Processor Splitter [Disagreement, with, beliefs, is, by, definition, incorrect] *///:~
3.2 adapter design mode
The Adapter Pattern serves as a bridge between two incompatible interfaces. This type of design pattern belongs to structural pattern, which combines the functions of two independent interfaces.
You can refer to the blog: Adapter mode | rookie tutorial (runoob.com)
3.3 factory mode
In factory mode, when creating objects, we do not expose the creation logic to the client, and point to the newly created objects by using a common interface.
You can refer to the blog: Factory mode | rookie tutorial (runoob.com)
Reference:
Java programming ideas