Look straight at ta! Look straight at ta! You must learn the interface well!

catalogue 1. Introduction: 2. Interface features: 3. Memb...
1. Introduction:
2. Interface features:
3. Member characteristics:
4. Case:
5. Interface and class similarities:
6. Differences between interfaces and classes:
7. Characteristics of interface:
8. The difference between abstract classes and interfaces
9. Tamping foundation:

catalogue

1. Introduction:

2. Interface features:

3. Member characteristics:

4. Case:

5. Interface and class similarities:

6. Differences between interfaces and classes:

7. Characteristics of interface:

8. The difference between abstract classes and interfaces

9. Tamping foundation:

1. Introduction:

         Everyone roars. Today I'd like to introduce you to a new friend - interface.

         Interface is a public standard, which can be used as long as it conforms to the standard. Interfaces in Java are more embodied in the abstraction of behavior. A class inherits the abstract methods of the interface by inheriting the interface.

         Interfaces are not classes. The way of writing interfaces is very similar to classes, but they belong to different concepts. Class describes the properties and methods of an object. Interface contains the methods to be implemented by the class. Unless the class implementing the interface is an abstract class, the class defines all methods in the interface.

         The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class. In addition, in Java, interface types can be used to declare a variable. They can become a null pointer or be bound to an object implemented by this interface.

2. Interface features:

The interface is decorated with the keyword interface:

         public interface interface name {}

Class implementation interface is represented by implements:

         public class class name implements interface name {}

Interface cannot be instantiated:

         How is the interface instantiated? Interface polymorphism refers to the implementation of class object instantiation by referring to polymorphism. Forms of polymorphism: concrete class polymorphism, abstract class polymorphism, interface polymorphism.

Subclass of interface:

         Either override all abstract methods in the interface, or the subclass is also an abstract class.

3. Member characteristics:

Member variables:

         Can only be constants. The default modifier is: public static final

Construction method:

         No, because the interface mainly extends functions, but does not exist.

Member method:

         Interface can only be abstract methods. The default modifier is: public abstract   . Static methods can be provided after JDK8, and default methods can be provided after JDK9. We will learn them later.

4. Case:

         After training cats and dogs, they can jump. Here we add the function of high jump. Please use abstract classes and interfaces to implement the cat case and test it in the test class.

Animals

public abstract class Animal { private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); }

High jump interface

public interface Jumpping { public abstract void jump(); }

Cats

public class Cat extends Animal implements Jumpping { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Cats eat fish"); } @Override public void jump() { System.out.println("The cat can jump high"); } }

Test class

public class AnimalDemo { public static void main(String[] args) { //Create an object and call a method Jumpping j = new Cat(); j.jump(); System.out.println("--------"); Animal a = new Cat(); a.setName("Garfield"); a.setAge(5); System.out.println(a.getName()+","+a.getAge()); a.eat(); a = new Cat("Garfield",5); System.out.println(a.getName()+","+a.getAge()); a.eat(); System.out.println("--------"); Cat c = new Cat(); c.setName("Garfield"); c.setAge(5); System.out.println(c.getName()+","+c.getAge()); c.eat(); c.jump(); } }

5. Interface and class similarities:

  • An interface can have multiple methods.
  • The interface file is saved in a file ending in. java, and the file name uses the interface name.
  • The bytecode file of the interface is saved in the file at the end of. class.
  • The bytecode file corresponding to the interface must be in the directory structure matching the package name.

6. Differences between interfaces and classes:

  • Interfaces cannot be used to instantiate objects.
  • Interface has no constructor.
  • All methods in the interface must be abstract methods. After Java 8, you can use the default keyword to modify non abstract methods in the interface.
  • An interface cannot contain member variables except static and final variables.
  • Interfaces are not inherited by classes, but implemented by classes.
  • The interface supports multiple inheritance.

7. Characteristics of interface:

  • Each method in the interface is also implicitly abstract, and the methods in the interface will be implicitly specified as   Public abstract (it can only be public abstract, and other modifiers will report errors).
  • The interface can contain variables, but the variables in the interface will be implicitly specified as   public static final   Variable (and can only be public. If you modify it with private, you will report a compilation error).
  • The methods in the interface cannot be implemented in the interface. The methods in the interface can only be implemented by the class that implements the interface.

8. The difference between abstract classes and interfaces

  • 1. A method in an abstract class can have a method body, that is, it can realize the specific functions of the method, but the methods in the interface can't.
  • 2. The member variables in the abstract class can be of various types, while the member variables in the interface can only be   public static final   Type.
  • 3. The interface cannot contain static code blocks and static methods (Methods decorated with static), while the abstract class can have static code blocks and static methods.
  • 4. A class can only inherit one abstract class, but a class can implement multiple interfaces.

9. Tamping foundation:

          1. There is a display interface Graphoscope, which has the display method. There are two classes: desktop display and liquid crystal display implement the display interface, and a display manufacturer can produce these two kinds of displays; It has Computer class and Graphoscope attribute. It produces two computers, which are configured with desktop display and LCD display respectively.

//Interface public interface Graphoscope { void display(); } //Implementation class public class LCD implements Graphoscope{ @Override public void display() { System.out.println("Liquid crystal display"); } } //Implementation class public class ConsoleDisplay implements Graphoscope { @Override public void display() { System.out.println("Desktop display"); } } public class Computer { private Graphoscope gs; public Computer() { } public Computer(Graphoscope gs) { this.gs = gs; } public Graphoscope getGs() { return gs; } public void setGs(Graphoscope gs) { this.gs = gs; } } //Test class public class Test { public static void main(String[] args) { Computer c = new Computer(new ConsoleDisplay()); Computer d = new Computer(new LCD()); c.getGs().display(); d.getGs().display(); } }

        two   There is a vehicle interface Vehicles, a work method, a Horse class and a Boat class to implement Vehicles respectively, create a vehicle factory class, and two methods to obtain vehicle Horse and Boat respectively; There are Person class, name and Vehicles attributes, which are assigned in the constructor to instantiate "Tang Monk". Generally, Horse is used as the vehicle, and Boat is used as the vehicle when encountering a river.

public interface Vehicles { void work(); } public class Horse implements Vehicles{ @Override public void work() { System.out.println("Tang Monk pedaled on his horse"); } } public class Boat implements Vehicles{ @Override public void work() { System.out.println("Tang Monk drifted by boat"); } } public class VehicleFactory { public Horse horseProduct() { return new Horse(); } public Boat BoatProduct() { return new Boat(); } } public class People { private String name; private Vehicles v; public People() { } public People(String name, Vehicles v) { this.name = name; this.v = v; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Vehicles getV() { return v; } public void setV(Vehicles v) { this.v = v; } } public class Test { public static void main(String[] args) { VehicleFactory v = new VehicleFactory(); System.out.println("Tang monk met Tongtian River"); People t = new People("Tang Monk", v.BoatProduct()); t.getV().work(); System.out.println("Tang Monk crossed the river"); t = new People("Tang Monk", v.horseProduct()); t.getV().work(); } }

Please pay attention! Please comment! Please praise!  

28 September 2021, 22:10 | Views: 5238

Add new comment

For adding a comment, please log in
or create account

0 comments