Java polymorphism--a case that thoroughly understands it

Source: Internet
Author: User

Original: https://www.cnblogs.com/1693977889zz/p/8296595.html

Recently, it's really important to find the basics. For example, the characteristics of polymorphism in Java, in learning is difficult to understand, more abstract concepts. Learn to be confused, but in the future will find that the foundation in the daily work of understanding occupies an important role.

Below, I will use a code instance to recall and consolidate the concept of polymorphism and the meaning of existence. Understanding polymorphism is an important step in object-oriented programming.

Let's start with a warm-up and look at the difference between 1 and 2 when the Mian function is called. 2 is the use of polymorphic thinking, this is the most simple small chestnut. An is equivalent to a reference (type), but it can represent multiple states.

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:

Scenario Assumptions:

A host of cats and dogs, cats and dogs have their own favorite food, the host when feeding them, if both the judge is a cat or a dog, and then judge what they love to eat, it seems very troublesome. If the owner raises many kinds of animals, such repeated judgment, will waste a lot of time. There is no way, can let the master to get a kind of food to know this is what kind of animal, good.

A complete code example:

1. First, the creation of animal species:

Animal Class class Animal {    int age;    String name;    public int getage () {        return age;    }    public void Setage (int.) {        this.age = age;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    There are two methods of calling and eating inside the animal class public    void Cry () {        System.out.println ("I don't know what to call");    }    public void Eat () {        System.out.println ("I don't know what to eat");}    }

2. Next, create Cat and dog categories (they inherit from animals):

Dog class inherits from animal class dog extends Animal {    //overwrite (override) method public    void Cry () {        System.out.println ("Mong Wang");    }    public void Eat () {        System.out.println ("I am a dog, I love to eat Bones");}    } Cat class inherits from animal class Cat extends Animal {    //overwrite (override) method public    void Cry () {        System.out.println ("Meow meow");    }    public void Eat () {        System.out.println ("I am a cat, I love to eat Fish");}    }

3. In addition, create food classes:

Food class Foods {    String name;    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Food class inside Let it have a method public    void ShowName () {    }}

4. In addition, cats and dogs have their own different food to eat (they inherit from the food category):

Fish (a kind of food) inherited from food class fish extends foods {public    void ShowName () {        System.out.println ("Food: Fish");}    Bone (a kind of food) inherited from food class Bone extends foods {public    void ShowName () {        System.out.println ("Food: Bones");}    

5. The main human being (it is possible to unify the animals and the corresponding food):

There is a feeding method for the main human. Class Master {//Feed    The animal, if not polymorphic, he will write to the cat feeding and feeding the dog two methods    //have polymorphic, even after a lot of animals, with this function can be public    void Feed (Animal an, food f) {        an.eat ();        F.showname ();    }}

6. Finally, the invocation of the Method (test):

public class Duotaidemo {public    static void Main (String args[]) {        Master master = new Master ();        Master.feed (New Dog (), New Bone ());        Hin is convenient, you can try again        master.feed (New Cat (), New Fish ());    }}

Summary: three prerequisites for polymorphic presence (1, to have inheritance, 2, to have overrides, 3, to refer to a subclass object for a parent class reference)

Java polymorphism--a case that thoroughly understands it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.