1. polymorphic Concept
Polymorphism is the embodiment of multiple forms of objects. For example, we say "pet" This object, it has a lot of different expression or implementation, such as a kitten, puppies, lizards and so on. So I went to the pet shop and said, "Please give me a pet," the waiter gives me a kitten, a puppy or a lizard, and we say that the object of "pet" is polymorphic.
Polymorphism in Java refers to the multiple states of a reference (type) in different situations. It can also be understood that polymorphism refers to a method that is implemented in a different subclass by pointing to a pointer to the parent class. It can also be understood as "one interface, multiple methods".
There are two ways to implement polymorphism: 1, inheritance, 2, interface
Case one:
Polymorphic Demo [Dome122.java]//demonstrate inheritance, method overrides, Polymorphic Public classDemo122 { Public Static voidMain (string[] args) {//non-polymorphic, creating an object, and then calling its methodCat cat=NewCat (); Cat.cry (); Dog Dog=NewDog (); Dog.cry (); //a polymorphic mechanism that invokes a method of a subclass from an object created by the parent class, known as an interface, and multiple methodsAnimal an=NewCat (); An.cry ();//show the cat barkingan=NewDog (); An.cry ();//Show Barking }}//Animal Classclassanimal{String name; intAge ; To allow external methods to access properties inside the class (private), you need to add the properties inside the class get and set methods
PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } //animals will call Public voidcry () {System.out.println ("Don't know what to call it."); }}//Create a dog subclass and inherit the animal parent class and overwrite the Cry methodclassDogextendsanimal{//The dog barks . Public voidcry () {System.out.println ("Bark."); }}classCatextendsanimal{//Cat Bark Public voidcry () {System.out.println ("Cat's name."); }}
Case TWO:
/*
multiple polymorphic demo [Dome123.java]* /
//Demo subclass Inherits Parent class, method override, Polymorphic method Public classDemo123 { Public Static voidMain (string[] args) {//Non -polymorphic demoSYSTEM.OUT.PRINTLN ("Non-polymorphic Demo:"); Cat Cat=NewCat (); Cat.cry (); Dog Dog=NewDog (); Dog.cry (); System.out.println (); //Multi-State demoSystem.out.println ("polymorphic Demo:"); Animal an=NewCat (); An.cry (); an=NewDog (); An.cry (); System.out.println (); //Multi -Modal presentationSYSTEM.OUT.PRINTLN ("Multiple polymorphic Demo:"); Master Master=NewMaster (); Master.feed (NewDog (),NewBone ()); Master.feed (NewCat (),NewFish ()); }}//Lord Humanclassmaster{//to feed animals, use polymorphism, just write a method Public voidFeed (Animal an,food f) {an.eat (); F.showname (); }}//Food Parent Classclassfood{String name; Public voidShowName () {System.out.println (Food); }}//Food Roe ClassclassFishextendsfood{ Public voidShowName () {System.out.println (Fish); }}//Food Bone sub-categoryclassBoneextendsfood{ Public voidShowName () {System.out.println (Bones); }}//Animal class Animal Parent classclassanimal{String name; intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } //animals will call Public voidcry () {System.out.println ("Don't know what to call it."); } //animals eat. Public voideat () {System.out.println ("I don't know what to eat."); }}//Create a dog subclass and extends inherit animal parent class and Overwrite cry methodclassDogextendsanimal{//The dog barks . Public voidcry () {System.out.println ("Bark."); } //The dog eats. Public voideat () {System.out.println ("Dogs love Bones."); }}classCatextendsanimal{//The cat called himself Public voidcry () {System.out.println ("Cat's name."); } //The cat eats. Public voideat () {System.out.println ("Cats love fish."); }}
polymorphic -- Precautions :
1. Java allows a reference variable of the parent class to refer to an instance of its subclass (object)
Animal an=New Cat (); // This conversion is done automatically by the