0x00 overview
The knowledge points of Java involved in this paper are polymorphism, abstract class and interface
0x01 polymorphism
1.1 overview of polymorphism
- What is polymorphism
The same object shows different forms at different times
- Polymorphic premise
There should be inheritance or implementation relationship
There should be method rewriting
A parent class reference should point to a child class object
1.2 member access characteristics in polymorphism
- Member access characteristics
Member variable: compile to see the parent class, run to see the parent class
Member method: compile to see the parent class and run to see the child class
Example
package com.molyTest1; public class Animal { public int age = 40; public void eat() { System.out.println("Animals eat"); } }
package com.molyTest1; public class cat extends Animal { public int age = 20; public int weight = 10; @Override public void eat() { System.out.println("Cats eat fish"); } public void playGame() { System.out.println("Cat hide and seek"); } }
package com.molyTest1; public class AnimalDemo { public static void main(String[] args) { // A parent class reference points to a child class object Animal a = new cat(); System.out.println(a.age); // System.out.println(a.weight); a.eat(); //a.playGame(); } }
output
40 Cats eat fish
1.3 benefits and disadvantages of polymorphism
- Benefits:
The extensibility of the provider. When defining a method, the parent type is used as a parameter. When using, the specific subclass line is used to participate in the operation
- Disadvantages:
Unique members of subclasses cannot be used
1.4 transformation in polymorphism
- Upward transformation
A parent class reference to a child class object is an upward transformation
- Downward transformation
Format: subclass row object name = (subclass row) parent class reference;
Example
package com.molyTest2; public class Animal { public void eat() { System.out.println("Animals eat"); } }
package com.molyTest2; public class Cat extends Animal { @Override public void eat() { System.out.println("Cats eat fish"); } public void playGame() { System.out.println("Cat hide and seek"); } }
package com.molyTest2; public class AnimalDemo { public static void main(String[] args) { // polymorphic // Upward transformation Animal a = new Cat(); a.eat(); //a.playGame(); Cat c = (Cat)a; c.eat(); c.playGame(); } }
1.5 polymorphic cases
- Case requirements
Please use the idea of polymorphism to realize the case of cat and dog, and test it in the test class
Example
package com.molyTest3; public 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 void eat() { System.out.println("Animals eat"); } }
package com.molyTest3; public class Cat extends Animal { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Cats eat fish"); } public void playGame() { System.out.println("Cat hide and seek"); } }
package com.molyTest3; public class Dog extends Animal { public Dog() { } public Dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Dogs eat bones"); } }
package com.molyTest3; public class AnimalDemo { public static void main(String[] args) { // Create cat objects for testing Animal a = new Cat(); a.setName("Garfileds"); a.setAge(3); System.out.println(a.getName()+", "+a.getAge()); a.eat(); a = new Dog("Echo",5); System.out.println(a.getName()+", "+a.getAge()); a.eat(); } }
0x02 abstract class
2.1 overview of abstract classes
When we extract the common functions of subclasses, some methods have no specific implementation in the parent class, so we need to abstract classes.
In Java, a method without a method body should be defined as an abstract method, and if there is an abstract method in a class, the class must be an abstract class.
2.2 characteristics of abstract classes
- Abstract classes and methods must be decorated with the abstract keyword
// Definition of abstract class public abstract class Class name {} // Definition of abstract methods public abstract void eat();
- Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes
- Abstract classes cannot be instantiated
How are abstract classes instantiated? Abstract class polymorphism refers to the way of instantiation through subclass objects, which is called abstract class polymorphism
- Subclasses of abstract classes
Or override all abstract methods in an abstract class
Or abstract classes
2.3 member characteristics of abstract classes
- Characteristics of members
Member variable: it can be either a variable or a constant
Construction method: empty parameter construction and parametric construction
Member method: abstract method, ordinary method
Example
package com.abstractTest1; public abstract class Animal { private int age = 20; private final String city = "Beijing"; public Animal() { } public Animal(int age) { this.age = age; } public void show() { age = 40; System.out.println(age); System.out.println(city); } public abstract void eat(); }
package com.abstractTest1; public class Cat extends Animal { @Override public void eat() { System.out.println("Cats eat fish"); } }
package com.abstractTest1; public class AnimalDemo { public static void main(String[] args) { Animal a = new Cat(); a.eat(); a.show(); } }
output
package com.abstractTest1; public class AnimalDemo { public static void main(String[] args) { Animal a = new Cat(); a.eat(); a.show(); } }
2.4 cases of abstract classes
Case requirements: please use the idea of abstract classes to implement the cat and dog cases and test them in the test class
Example
package com.abstractTest2; 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(); }
package com.abstractTest2; public class Cat extends Animal { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.err.println("Cats eat fish"); } }
package com.abstractTest2; public class Dog extends Animal { public Dog() { } public Dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Dogs eat bones"); } }
package com.abstractTest2; public class AnimalDemo { public static void main(String[] args) { // Create objects in a polymorphic manner Animal a = new Cat(); a.setName("Garfields"); a.setAge(5); System.out.println(a.getName()+","+a.getAge()); a.eat(); System.out.println("-------------"); a = new Cat("Garfields", 5); System.out.println(a.getName()+", "+a.getAge()); a.eat(); } }
0x03 interface
3.1 Interface Overview
An interface is a public specification mark. As long as it meets the specifications and standards, everyone can use it
The interface in Java is more embodied in the abstraction of behavior
3.2 characteristics of interface
- The interface is decorated with the keyword interface
public interface Interface name {}
- The 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
- Interface subclass
Or override all abstract methods in the interface
Either subclasses are abstract classes
3.3 characteristics of interface members
- Member characteristics
Member variable: can only be constant, default modifier, public static final
Construction method: No, because the interface mainly extends functions, but does not exist
Member methods: can only be abstract methods. The default modifier is public abstract. For methods in interfaces, there are some new features in JDK8 and 9, which will not be explained here
Example
package com.interfaceTest1; public interface Inter { public int num = 10; public final int num2 = 20; // public static final int num3 = 30; int num3 = 30; //public Inter() {} //public void show() {} // public abstract void method(); void show(); }
package com.interfaceTest1; public class InterImpl implements Inter { public InterImpl() { super(); } @Override public void method() { System.out.println("method"); } @Override public void show() { System.out.println("show"); } }
package com.interfaceTest1; public class InterfactDemo { public static void main(String[] args) { Inter i = new InterImpl(); // i.num = 20; System.out.println(i.num); //i.num2 = 40; System.out.println(i.num2); System.out.println(Inter.num); } }
output
10 20 10
3.4 interface cases
Case requirements:
Train cats and dogs. They can jump high. Add the function of high jump
Please use abstract classes and interfaces to implement the cat and dog case and test it in the test class
Example
package com.interfaceTest2; 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(); }
package com.interfaceTest2; public interface Jumpping { public abstract void jump(); }
package com.interfaceTest2; 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 be raised"); } }
package com.interfaceTest2; 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("Garfields"); a.setAge(3); System.out.println(a.getName()+", "+a.getAge()); a.eat(); // a.jump(); a = new Cat("Aria", 6); System.out.println(a.getName()+", "+a.getAge()); a.eat(); // a.jump(); System.out.println("----------"); Cat c1 = new Cat(); c1.setName("Garfield"); c1.setAge(7); System.out.println(a.getName()+", "+a.getAge()); c1.eat(); c1.jump(); } }
output
The cat can be raised ---------- Garfields, 3 Cats eat fish Aria, 6 Cats eat fish ---------- Aria, 6 Cats eat fish The cat can be raised
3.5 relationship between class and interface
- Relationship between classes
Inheritance relationship can only be inherited in single layer, but it can be inherited in multiple layers
- Relationship between class and interface
The implementation relationship can be implemented alone or multiple, and multiple interfaces can be implemented while inheriting a class
- Interface and interface relationship
Inheritance relationship can be single inheritance or multiple inheritance
3.6 differences between interfaces of abstract classes
- Member differences:
Abstract classes: variables and constants. There are construction methods, abstract methods and non abstract methods
Interfaces: constants, abstract methods
- Relationship differences:
Classes and classes: inheritance, single inheritance
Class and interface: implementation, single or multiple
- Interface and interface
Abstract classes: abstract classes, including properties and behaviors
Interface: abstract behavior, mainly behavior
0x04 comprehensive case
4.1 case requirements
We first have table tennis players and basketball players, table tennis coaches and basketball coaches.
In order to communicate abroad, people related to table tennis need to learn English.
Please use your knowledge to analyze which specific classes, which abstract classes and which interfaces are in this case, and implement them in code.
4.2 code implementation
package com.intergratedPractise; public abstract class Person { private String name; private int age; public Person() { } public Person(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(); }
package com.intergratedPractise; public abstract class Player extends Person { public Player() { } public Player(String name, int age) { super(name, age); } public abstract void study(); }
package com.intergratedPractise; public abstract class Coach extends Person { public Coach() { } public Coach(String name, int age) { super(name, age); } public abstract void teach(); }
package com.intergratedPractise; public interface speakEnglish { public abstract void speak(); }
package com.intergratedPractise; public class basketBallCoach extends Coach{ public basketBallCoach() { } public basketBallCoach(String name, int age) { super(name, age); } @Override public void teach() { System.out.println("The basketball coach teaches how to dribble and shoot"); } @Override public void eat() { System.out.println("Basketball coaches eat mutton and drink goat milk"); } }
package com.intergratedPractise; public class pingPongCoach extends Coach implements speakEnglish{ public pingPongCoach() { } public pingPongCoach(String name, int age) { super(name, age); } @Override public void teach() { System.out.println("The table tennis coach teaches how to serve and receive the ball"); } @Override public void eat() { System.out.println("Table tennis coaches eat beef and drink milk"); } @Override public void speak() { System.out.println("The table tennis coach speaks English"); } }
package com.intergratedPractise; public class BasketBallPlayer extends Player{ public BasketBallPlayer() { } public BasketBallPlayer(String name, int age) { super(name, age); } @Override public void study() { System.out.println("Basketball players learn how to dribble and shoot"); } @Override public void eat() { System.out.println("Basketball players eat mutton and drink goat milk"); } }
package com.intergratedPractise; public class PingPongPlayer extends Player implements speakEnglish{ public PingPongPlayer() { } public PingPongPlayer(String name, int age) { super(name, age); } @Override public void study() { System.out.println("Table tennis players learn how to serve and receive the ball"); } @Override public void eat() { System.out.println("Table tennis players eat beef and drink milk"); } @Override public void speak() { System.out.println("Table tennis players speak English"); } }