Polymorphism – Concept
The so-called polymorphic, refers to a reference (type) in different situations in a variety of states. 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.
There are two ways to implement polymorphism: 1, inheritance, 2, interface
This time, let's start by demonstrating:
inheritance, method coverage, polymorphism, and up-conversion content
Package polimorphic; Public class polimorphic { Public Static voidMain (string[] args) {//Non -polymorphic demoSystem.out.println ("Non-polymorphic Demo:"); Cat cat=NewCat (); Cat.cry ();//cat=new Dog (); Types do not matchDog dog=NewDog (); Dog.cry (); Dog.bite (); System.out.println ();//Multi-state demoSystem.out.println ("polymorphic Demo:"); Animal an=NewCat (); An.cry (); an=NewDog (); An.cry ();//an.bite (); An is a sub-class object but loses the bite () method at this pointSystem.out.println ();//Multi-modal demoSystem.out.println ("Multi-Modal Demo:"); Master master=NewMaster (); Master.feed (NewDog (),NewBone ()); Master.feed (NewCat (),NewFish ()); }}//Master human class Master{ //Give animal feed, use polymorphism, just write a method Public voidFeed (Animal An,food f) {an.eat (); F.showname (); }}//Food parent class class food {String name; Public voidShowName () {System.out.println ("Food"); }}//Food Roe class class Fish extends food { Public voidShowName () {//Override parent class methodSystem.out.println ("Fish"); }}//Food Bone sub-category class Bone extends food { Public voidShowName () {//Override parent class methodSystem.out.println ("Bones"); }}//Animal class Animal Parent class class Animal{String name;intAge PublicString GetName () {returnName } Public voidSetName (String name) { This. name = name; } Public intGetage () {returnAge } Public voidSetage (intAge) { This. Age = Age; }//animals will be called Public voidCry () {System.out.println ("Don't know what to call it."); }//animals eat things 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 method class Dog extends Animal{ //Dog Bark Public voidCry () {//Override parent class methodSystem.out.println ("bark."); }//dog eats something Public voidEat () {//Override parent class methodSystem.out.println ("Dogs love bones."); } Public voidBite () {System.out.println ("The dog will bite ..."); }} class Cat extends Animal{ //Cats call themselves Public voidCry () {//Override parent class methodSystem.out.println ("Cat's name."); }//cat eats something Public voidEat () {//Override parent class methodSystem.out.println ("Cats love fish."); }}
Results
Polymorphism – 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.
Transformation is based on inheritance, which is a mechanism of code reuse in object-oriented languages, through inheritance, subclasses can reuse the function of the parent class, and if the parent class does not meet the requirements of the current subclass, the subclass can override the methods in the parent class to extend it.
Transition up: The object that the subclass refers to is converted to a parent class type called upward transformation. In layman's words, a subclass object is converted to a parent class object. Here the parent object can be an interface
The Animal Dog reference points to New dog (), the subclass object as a parent object, only the members of the parent class, and if the subclass overrides the method of the parent class, it points to this method of overriding the calling subclass (this method overrides override). This calling procedure is called dynamic binding.
Issues to be aware of in transition:
When you move up, the parent class points to the subclass reference object and loses other methods that are common to the parent object, that is, during the transformation, the new methods of the subclass are lost, and at compile time, the system provides an error that the method cannot be found.
For example: the above code
//an.bite(); // an虽指向子类对象,但此时丢失bite()方法
There are some specific details about the type conversion requirements, which we would like to mention later, such as whether the subclass can be converted to a parent class, what requirements, etc...
The problem of downward transformation is discussed in the next section.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Multi-State learning 1