Detailed explanation of java interface + Exercises (you can see it at the beginning)

1, Interface Overview
Interface is a higher abstraction than abstraction. It is a completely abstract class, that is, a collection of abstract methods. Interfaces are declared using the keyword interface.

2, Structure of interface definition
[modifier] interface interface name {
Abstract method 1;
Abstract method 2;

Example: define a Country interface with a nationality attribute and a speak method

interface Interface name // Define abstract classes
{
    final Data type member name = Constant; //Data members must be assigned initial values
    abstract Data type and method name (parameter) of the return value...);
    //Abstract method. Note that there is no method body defined in the abstract method
}

Precautions for interface definition:

  • There are also variables in the interface, but the interface will be implicitly specified as a public static final variable

  • Abstract methods in the interface have public and abstract modifiers, which can only be used. Other modifiers will report errors-

  • Abstract classes can inherit. Similarly, interfaces can inherit. When inheriting between interfaces, the keyword used is also extends

  • The "abstract method" in the interface can be declared without defining its processing method;

  • Data members must be assigned initial values

3, Implementation of interface
When a class implements an interface, the class must implement all the methods in the interface, otherwise the class must be declared as an abstract class. The class uses the keyword implements to implement the interface.

give an example:
be careful:

  1.  the interface cannot be instantiated directly. The interface is implemented through classes
  2.  a class can implement multiple interfaces, which are separated by commas
  3.  if the class implementing an interface is not an abstract class, the class must implement all abstract methods of the specified interface
  4.  abstract classes can also implement interface (for example) implementations
  5.  multiple interfaces can be inherited, for example:
    *Interfaces are called inheritance extensions. The relationship between abstract classes and interfaces is to implement implements. The relationship between classes and interfaces is also to implement implements
interface IA{ 
void ma(); 
} 
interface IB{ 
void mb(); 
} 
interface IC extends IA, IB{ //IC inherits both IA and IB interfaces 
void mc(); 
} 

4, Interface and abstract class
1. Similarities between interface and abstract class
 can be inherited
 cannot be instantiated directly
 can contain abstract methods
 derived classes must implement unimplemented methods

2. Differences between interface and abstract class
 interfaces support multiple inheritance, and abstract classes cannot implement multiple inheritance
 a class can only inherit one abstract class, but can implement multiple interfaces
 the member variables in the interface can only be of publci static final type, and the member variables in the abstract class can be of various types
 interfaces can only define abstract methods; abstract classes can define either abstract methods or ordinary methods
 the interface cannot contain static code blocks and static methods, and abstract classes can have static code blocks and static methods

5, Exercises understanding

package zhou;
public interface interfaceA {
    public abstract String shi();
    public abstract String shu();
}
class Student implements interfaceA{
    private String eatery;
    private String dorm;
    Student(String eatery,String dorm){
        this.eatery=eatery;
        this.dorm=dorm;
    }
    public String shi() {
        return "canteen"+eatery;
    }
    public String shu() {
        return "dormitory"+dorm;
    }
}
class Teacher implements interfaceA {
    private String teachereatery;
    private String schoolflat;
    Teacher(String teachereatery,String schoolflat){
        this.teachereatery=teachereatery;
        this.schoolflat=schoolflat;
    }
    public String shi() {
        return "Teacher canteen"+teachereatery;
    }
    public String shu() {
        return "School apartment"+schoolflat;
    }
}
class ParentsofStudents implements interfaceA{
    private String resthouse;
    ParentsofStudents(String resthouse){
        this.resthouse=resthouse;
    }
    public String shi() {
        return "Guesthouse"+resthouse;
    }
    public String shu() {
        return "Guesthouse"+resthouse;
    }
}
public class Hong {
    public static void main(String[] args) {
        Student s=new Student(null, null);
        s.shi();
        s.shu();
        Teacher t=new Teacher(null, null);
        t.shi();
        t.shu();
        ParentsofStudents j=new ParentsofStudents(null);
        j.shi();
        j.shu();
    }
}

6, Interface extension
Interface is a mechanism for Java to implement multi inheritance. A class can only inherit one parent class, but if a class needs to inherit multiple abstract methods, it obviously cannot be implemented. Therefore, the concept of interface appears. A class can inherit only one parent class, but can implement multiple interfaces.

Interfaces, like general classes, can derive new interfaces through extended technology. The original interface is called the basic interface or parent interface, and the derived interface is called the derived interface or child interface. Through this mechanism, the derived interface can not only retain the members of the parent interface, but also add new members to meet the actual needs.

Similarly, interface extension (or inheritance) is also implemented through the keyword extends. Interestingly, an interface can inherit multiple interfaces, which is different from class inheritance.
Syntax of interface extension:

**interface Sub interface name extends Parent interface 1,Parent interface 2...
{
    ...
}**

Tags: Java

Posted on Mon, 22 Nov 2021 20:08:40 -0500 by Kryllster