Tip: the following is the main content of this article, and the Java series learning will be continuously updated
(1) Basic concepts
an interface is a class that is more abstract than an abstract class. It can be regarded as a special abstract class. There are only global constants and abstract methods in the class body (before JDK8). The default method is extended in JDK8.
① the keyword for defining a class is class, and the keyword for inheriting a class is extends
The keyword that defines the interface is interface, and the keyword that implements the interface is implements
② the naming of general interface classes starts with I, and the subclasses implementing interfaces end with Impl, so as to distinguish them from ordinary classes.
③ there are only global constants and abstract methods in the interface.
when multiple types have the same behavior capabilities, the connection between types can be carried out through interfaces in java. The interface can solve the problem that some types cannot be shared caused by single inheritance in java.
the interface does not care about the internal data of the class or the implementation details of the methods in these classes. It only specifies some methods that must be provided in these classes.
(2) Syntax format
Access modifier interface Interface name { Definition of constants.........; Definition of abstract methods......; } public interface IMessage { //The constants of the interface are decorated with public static final, but can be omitted int NUM = 10; //The public abstract modifier can be omitted String message(); }
Access modifier class Subclass name implements Parent interface 1,Parent interface 2,... { Override of parent interface abstract method...; } public class MessageImpl implements Imessage { //Override the abstract method in the parent interface @Override public String message(){ return "Hello,Bit"; } }
(3) Basic principles
1. There are only public permissions in the interface; There are only global constants and abstract methods in the interface; In the interface, the abstract static final and public keywords can be omitted (in the Ali coding specification, the definition of the interface should be as concise as possible, and the above keywords do not need to be written)
2. Subclasses must override all abstract methods in the parent interface.
3. Interface. Subclasses can implement multiple parent interfaces at the same time.
public class MessageImpl implements IMessage,INews{}
4. There is no single inheritance limit for interfaces. Interfaces can inherit multiple parent interfaces using extensions.
interface A { void textA(); } interface B { void textB(); } interface C extends A,B { void textC(); }
4. Subclasses can inherit the parent class and implement interfaces at the same time.
public class DTest extends D implements A,B {}
(4) Precautions
1. Interface cannot create object.
2. Interfaces can implement multiple inheritance, that is, an interface can inherit multiple parent interfaces at the same time.
3. If the class implementing the interface cannot implement the methods to be overridden in all interfaces, it must be set as an abstract class.
4. A class can inherit from a parent class and implement multiple interfaces at the same time.
(5) Case
IRun interface class
//Interface of animal behavior: running public interface IRun { void run(); }
ISwim interface class
//Interface of animal behavior: swimming public interface ISwim { void swim(); }
IFly interface class
//Interface of animal behavior: Flying public interface IFly { void fly(); }
Animal abstract class
//Define animal abstract classes public abstract class Animal { protected String name; public Animal(String name) { this.name = name; } //Abstract method: eat public abstract void eat(String food); }
Dog class: implementing inheritance and interfaces
//Dogs have the behavior of running public class Dog extends Animal implements IRun{ public Dog(String name) { super(name); } @Override public void eat(String food) { System.out.println("Dog" + this.name + "Yes"+ food); } @Override public void run() { System.out.println("Dog" + this.name + "Running happily~~"); } }
Frog class: implementing inheritance and interfaces
//Frogs have the behavior of jumping and swimming public class Frog extends Animal implements IRun,ISwim{ public Frog(String name) { super(name); } @Override public void eat(String food) { System.out.println(this.name + "Yes" +food); } @Override public void run() { System.out.println(this.name + "Jumping by the river"); } @Override public void swim() { System.out.println(this.name + "He is breaststroke"); } }
Goose class: implementing inheritance and interfaces
//Geese can walk, swim and fly public class Goose extends Animal implements IRun,ISwim,IFly { public Goose(String name) { super(name); } @Override public void eat(String food) { System.out.println(this.name + "Yes" + food); } @Override public void fly() { System.out.println(this.name + "Flying a little~~"); } @Override public void run() { System.out.println(this.name + "Flapping his wings~~"); } @Override public void swim() { System.out.println(this.name + "Swimming in the water"); } }
Test class: main() method test
public class Test { public static void main(String[] args) { Dog dog = new Dog("Xiao Liu"); dog.eat("Dog food"); dog.run(); System.out.println("---------------"); Frog frog = new Frog("Small frog"); frog.eat("insect"); frog.run(); frog.swim(); System.out.println("---------------"); Goose goose = new Goose("Ferocious goose"); goose.eat("Shrimp "); goose.run(); goose.swim(); goose.fly(); } }
Operation results:
Go back to the directory
(6) Two built-in interfaces of JDK
①java.lang.Comparable
Student is a user-defined class when using arrays When sort() sorts user-defined types, user-defined types need to implement the Comparable interface to enable them to compare.
package java.lang; public interface Comparable<T> { @Contract(pure = true) public int compareTo(@NotNull T o); }
Case:
/** * Implement Java Lang.comparable interface */ import java.util.Arrays; public class Student implements Comparable<Student> { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } // At this time, compare according to age @Override public int compareTo(Student o) { if (this.age == o.age) { return 0; }else if (this.age < o.age) { return -1; } return 1; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } public static void main(String[] args) { Student stu1 = new Student("Xiaobai",18); Student stu2 = new Student("Xiao Hei",20); Student stu3 = new Student("ginger",30); Student[] students = {stu3,stu1,stu2}; // Array sorting algorithm, sort in ascending order Arrays.sort(students); System.out.println(Arrays.toString(students)); } }
②java.lang.Cloneable
Copy a new object, and the attribute value of the new object is copied from the old object.
package java.lang; public interface Cloneable { }
this is a tag interface without any abstract methods. When a class implements the clonable interface, it means that the class has the cloning ability (given by the JVM).
After the interface is implemented, the clone() method in the Object class needs to be overridden internally
@Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }
Case:
/** * Implement Java Lang. clonable interface */ public class Animal implements Cloneable{ private String name; //Override the clone() method in the Object class @Override protected Animal clone() throws CloneNotSupportedException { return (Animal) super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Animal animal1 = new Animal(); animal1.name = "panda"; Animal animal2 = animal1.clone(); //Just copy one, not the same object System.out.println(animal1 == animal2); System.out.println(animal2.name); } }
(7) The difference between abstract classes and interfaces
No | difference | Abstract class | Interface |
---|---|---|---|
1 | Structural composition | Ordinary class + abstract method | Abstract method + global constant |
2 | jurisdiction | Various permissions | public |
3 | Subclass use | Using extensions to inherit abstract classes | Inheriting interfaces using implements |
4 | relationship | An abstract class can implement several interfaces | Interfaces cannot inherit abstract classes, but interfaces can inherit multiple parent interfaces using extensions |
5 | Subclass restrictions | A subclass can inherit only one abstract class | A subclass can implement multiple interfaces |
Summary:
Tip: here is a summary of the article:
The above is today's learning content. This article is to continue Java object-oriented learning, understand what interface classes are, learn syntax format and usage, understand the two built-in interfaces of JDK, and the differences between abstract classes and interfaces. The learning content will be updated continuously!!!