1. Polymorphism
1.1 what is polymorphism
The same object shows different forms at different times
1.2 premise of polymorphism
- There should be inheritance or implementation relationship (implementation relationship)
- There should be method rewriting
- A parent class reference should point to a child class object
Fu f = new Zi();
1.3 member access characteristics in polymorphism
- 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
1.4 advantages and disadvantages of polymorphism
- When defining a method, use the parent class as a parameter; When used, specific subtypes are used to participate in the operation
- Unique members of subclasses cannot be used
1.5 examples
public class Animal { public int age = 40; public void eat(){ System.out.println("Animals eat"); } } 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"); } } public class AnimalDemo { public static void main(String[] args) { Animal a = new Cat(); System.out.println(a.age); // Member variable: run to see parent class // System.out.println(a.weight); // report errors a.eat(); // Member method: run to see subclasses // a.playGame(); // report errors } }
Operation results:
40
Cats eat fish
1.6 transformation in polymorphism
// Animal and Cat are the same as 1.5 examples, and the following is the test class public class AnimalDemo { public static void main(String[] args) { Animal a = new Cat(); //1. Upward Transformation: the parent class reference points to the child class a.eat(); // Member method: run to see subclasses // a.playGame(); // report errors Cat c = (Cat) a; // Subtype name object name = (subtype name) parent class reference c.eat(); c.playGame(); } }
Operation results:
Cats eat fish
Cats eat fish
Cat hide and seek
2. Abstract class
2.1 abstract methods and abstract classes
- Abstract method: a method without a method body
- Abstract class: a class with abstract methods
2.2 definition of abstract classes and methods
- Definition of abstract class:
public abstract class name {} - Definition of abstract method:
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. They can be instantiated through subclass objects, which is called abstract polymorphism.
- Subclasses of abstract classes
Override all abstract methods in an abstract class, or subclasses are abstract classes.
2.3 examples
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(); } public class Cat extends Animal{ @Override public void eat() { System.out.println("Cats eat fish"); } } public class AnimalDemo { public static void main(String[] args) { Animal a = new Cat(); a.eat(); // Member method: run to see subclasses a.show(); } }
Operation results:
Cats eat fish
40
Beijing
3. Interface
3.1 interface definition
public interface interface name {}
3.2 class implementation interface
public class class name implements interface name {}
3.3 characteristics of interface
- If an interface cannot be instantiated, you can refer to the polymorphic method to instantiate class objects, which is called interface polymorphism.
- Forms of polymorphism: concrete class polymorphism, abstract class polymorphism, interface polymorphism
- Subclass of interface: override all abstract methods in the interface, or subclasses are also abstract classes
3.4 characteristics of interface members
- Member variables can only be constants. The default modifier is public static final
- No construction method
- Member methods can only be abstract methods. The default modifier is public abstract
3.5 case 1
public interface Inter { public int num = 10; public final int num2 = 20; // The member variable of an interface can be a constant public static final int num3 = 30; // Same as writing int num3 = 30 directly //public Inter() {} interface cannot have constructor //Public void show() {} / / there cannot be non abstract methods in the interface public abstract void method(); // Member methods in an interface can only be abstract void show(); // Methods in the interface take the public abstract modifier by default } public class Interimpl implements Inter{ // Implementation class of interface @Override public void method() { System.out.println("method"); } @Override public void show() { System.out.println("show"); } } public class InterfaceDemo { public static void main(String[] args) { Inter i = new Interimpl(); // Create objects in a polymorphic way // i.num = 20; // The member variables in the interface are modified by final by default and cannot be assigned values System.out.println(i.num); System.out.println(i.num2); //i.num2 = 40; It is modified by final and cannot be re assigned System.out.println(i.num2); System.out.println(Inter.num); // Members in an interface can be accessed directly through the interface name } }
Operation results:
10
20
20
10
3.6 cases 2
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 interface Jumpping { public abstract void jump(); } public class Cat extends Animal implements Jumpping { // Inherit the animal implementation interface 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(); // Interface polymorphism can only call methods in the interface System.out.println("----------"); Animal a = new Cat(); a.setName("Garfield"); a.setAge(5); System.out.println(a.getName()+","+a.getAge()); a.eat(); // Abstract class polymorphism can only be called to its methods // a.jump(); 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(); } }
Operation results:
The cat can jump high
-
Garfield, 5
Cats eat fish
Garfield, 5
Cats eat fish
-
Garfield, 5
Cats eat fish
The cat can jump high
3.7 relationship between class and interface
- Relationship between classes
Inheritance relationship can only be inherited in single layer, and can be inherited in multiple layers - Relationship between class and interface
The implementation relationship can be implemented in a single or multiple way, and multiple interfaces can be implemented while inheriting a class - Relationship between interfaces
Inheritance relationship, including single inheritance and multiple inheritance
3.8 relationship between abstract classes and interfaces
- Member difference
Abstract class: variable, constant; Construction method; Abstract method
Interface: constant; Abstract method - Abstract class is the abstraction of things, and interface is the abstraction of behavior