Getting started with JavaSE 21: Java object-oriented interface (interface) (2)
1. polymorphism of interface implementation
In the previous blog post: JavaSE getting started 20: Java object-oriented interface (interface) (I) mentioned that the interface implementation has polymorphism, so
This article mainly analyzes the polymorphism of interface implementation.
Instance 1
Test. java source file code:
Public class Test {public static void main (String [] args) {// implementation interface SingerSinger s1 = new Student ("Amy"); s1.sing (); s1.sleep (); s1.study (); // compilation error // implementation interface SingerSinger s2 = new Teacher ("Jack"); s2.sing (); s2.sleep (); s2.paint (); // compilation error s2.eat (); // compilation error s2.teach (); // compilation error // implementation interface PainterPainter p1 = (Painter) s2; p1.paint (); p1.eat (); p1.teach (); // compilation error p1.sing (); // compilation error p1.sleep (); // compilation error} // interface Singerinterface Singer {// abstract method public void sing (); public void sleep ();} // interface Painterinterface Painter {// abstract method public void paint (); public void eat () ;}// Student, inherit a Singer interface class Student implements Singer {// private member variable private String name; // constructor Student (String name) {this. name = name;} public String getName () {return name;} // study () method public void study () {System. out. println ("student is studying");} // rewrite the public void sing () {System. out. println ("student is singing");} // rewrite the sleep () method public void sleep () {System. out. println ("student is sleeping");} // extends class Teacher, inheriting two interfaces: Singer and Painterclass Teacher implements Singer, Painter {// private member variable private String name; // constructor Teacher (String name) {this. name = name;} public String getName () {return name;} // The teach () method public void teach () {System. out. println ("teacher is teaching");} // rewrite the sing () method public void sing () {System. out. println ("teacher is singing");} // rewrite the sleep () method of the Singer interface public void sleep () {System. out. println ("teacher is sleeping");} // rewrite the Painter's paint () method public void paint () {System. out. println ("teacher is painting");} // rewrite the eat () method public void eat () {System. out. println ("teacher is eating ");}}
An error occurred during compilation:
From the error message above, we can see that the reference polymorphism, method polymorphism, and
Using type conversion, these can also be implemented in the interface, but the difference is that the interface cannot be instantiated, but the interface reference can point to inherit it
Of course, the called method is also the method of the abstract method in the rewrite interface of the subclass. For more information about object polymorphism, see JavaSE.
Entry 18: Java object-oriented polymorphism.
The number of lines reported during the above compilation process is 7, 13, 14, 15, 21, 22, and 23. These error types cannot be found because the reference class
For example, if the Singer interface references the object s1 pointing to the Student of the subclass, this object reference can only include the Student of the subclass to override the interface.
The two abstract methods of Singer cannot include other abstract methods in the Painter interface to be rewritten, as well as the unique methods of the sub-class Student.
. To avoid this compilation error, we can create the Student subclass object and the Teacher subclass object.
Instance 2
Rewrite the code in the Test class:
Public class Test {public static void main (String [] args) {// create the Student class Object, implement the interface SingerStudent s1 = new Student ("Amy"); s1.sing (); s1.sleep (); s1.study (); // create a Teacher Class Object to implement interfaces Singer and PainterTeacher t1 = new Teacher ("Jack"); t1.sing (); t1.sleep (); t1.paint (); t1.eat (); t1.teach ();}}
Compile the running result:
Second Interface Usage (for actual reference) (1) Streamline the program structure and avoid repeated Definitions
For example, if two or more classes have the same method but different implementation functions, you can define an interface to extract this method,
Implementation in the class that requires this method eliminates the trouble of defining system methods for multiple classes.
For example, birds and insects have the same flying function, but other functions are different.
You can define an interface to specifically describe flight.
Is to define birds and insects respectively. They all have flight methods. The class diagram is:
The interface is defined, and its class diagram is as follows:
Specific Code implementation:
// Interface FlyAnimalinterface FlyAnimal {// abstract method fly () void fly ();} // Insect class Insect {int legnum = 6; void oviposition (){};} // Bird class Bird {int legnum = 2; void egg () {};}// Ant class Ant extends Insect implements FlyAnimal {public void fly () {System. out. println ("Ant can fly");} public void oviposition () {System. out. println ("Ant can spawn") ;}// Pigeon class Pigeon extends Bird implements FlyAnimal {public void fly () {System. out. println ("pigeon can fly");} public void egg () {System. out. println ("pigeon can lay eggs") ;}} public class Test {public static void main (String args []) {Ant a = new Ant ();. fly ();. oviposition (); System. out. println ("Ant's legs are" +. legnum); System. out. println ("\ n"); Pigeon p = new Pigeon (); p. fly (); p. egg (); System. out. println ("Pigeon's legs are" + p. legnum );}}
Compile the running result:
(2) Expand program functions to respond to changes in requirements
Assume that a school reception program is used to entertain people of different identities. The corresponding rules are as follows:
In theory, of course, you can define a corresponding class for each person of different identities and implement their own methods. However, observing this write class can be summarized as follows:
It has a common template, that is, the "food" and "Sleep" issues of "people. At this time, you can use the interface function.
The specific implementation code is as follows:
Interface Person {void eat (); void sleep ();} class Student implements Person {public void eat () {System. out. println ("students go to the canteen for dinner! ");} Public void sleep () {System. out. println (" students go back to bed! ") ;}} Class Teacher implements Person {public void eat () {System. out. println (" the instructor went to the tutor's restaurant for dinner! ");} Public void sleep () {System. out. println (" the instructor goes back to the school apartment to bed! ") ;}} Class Parents implements Person {public void eat () {System. out. println (" Parents go to the hostel for dinner! ");} Public void sleep () {System. out. println (" parents go back to the hostel to bed! ") ;}} Public class Test {public static void main (String [] args) {Person p1 = new Student (); p. eat (); p. sleep (); Person p2 = new Teacher (); p. eat (); p. sleep (); Person p3 = new Parents (); p. eat (); p. sleep ();}}
Running result:
Now you need to add some features, that is, you need to add "foreign guests, superiors" two types of roles, and later tools need to add the corresponding identity Angle
You only need to add "foreign guests" and "leaders" classes as needed, and the main class can still be used without further modification. This
The interface function is displayed.
Our list is updated:
In the above program, add the following two classes:
Class Foreign implements Person {public void eat () {System. out. println ("Foreign guests go to the hotel for dinner! ");} Public void sleep () {System. out. println (" foreign guests go to the hotel to bed! ") ;}} Class Leader implements Person {public void eat () {System. out. println (" lead to the hotel for dinner! ");} Public void sleep () {System. out. println (" lead back to the hotel to sleep! ");}}
The code we added in the main method is:
Person p4 = new Foreign(); p4.eat(); p4.sleep(); Person p5 = new Leader(); p5.eat(); p5.sleep();
Running result:
Summary