Polymorphism, polymorphismjava
Polymorphism definition (Baidu encyclopedia): Polymorphism (Polymorphism) literally means "multiple States ". In object-oriented language, multiple implementation methods of interfaces are polymorphism. Referencing Charlie Calverts's description of Polymorphism
-- Polymorphism is a technique that allows you to set a parent object to be equal to one or more other sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it (from "Delphi4 Programming Technology
"). To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Polymorphism is implemented by Virtual functions in Object Pascal and C ++.
As I learned today, Polymorphism is a parent type reference that can point to sub-objects. ("Subclass is the parent class ")
Interface Animal {
Void shout (); // define the abstract shout () method
}
// Define the Cat class to implement the Animal Interface
Class Cat implements Animal {
// Implement the shout () method
Public void shout (){
System. out. println ("meow ...... ");
}
}
// Define the Dog class to implement the Animal Interface
Class Dog implements Animal {
// Implement the shout () method
Public void shout (){
System. out. println ("Wang ...... ");
}
}
// Define the test class
Public class Example13 {
Public static void main (String [] args ){
Animal an1 = new Cat (); // creates a Cat object, which is referenced by an1, a variable of the Animal type.
Animal an2 = new Dog (); // creates a Dog object, which is referenced by an2.
AnimalShout (an1); // call the animalShout () method and pass an1 as a parameter
AnimalShout (an2); // call the animalShout () method and pass an2 as a parameter
}
// Defines the static animalShout () method and receives an Animal type parameter.
Public static void animalShout (Animal ){
An. shout (); // call the shout () method of the actual Parameter
}
}