Java understanding of Polymorphism

Source: Internet
Author: User

The basis of a polymorphic is inheritance, overriding, the reference of the parent class to the subclass object, and the interface is the most common way to implement polymorphism.

The concept of things, first said so much, the above is the focus, inheritance, rewrite, the parent class reference to the subclass object.

Put the code below, step by step to see what is polymorphic.

Parent class, and then there will be dog and cat classes that inherit this class

public class Dongwu {public void Eat () {System.out.println ("Animal Eats Things");} public void Ages () {System.out.println ("Age of Animal");}}

Dog class

public class Dog extends Dongwu {//Overrides Eat () method of the parent class public void Eat () {System.out.println ("Dog eats Bones");} Override the Age () method of the parent class public void age () {System.out.println ("10-year-old Dog");}

Cat class

public class Cat extends Dongwu {//Overrides the Eat () method of the parent class public void Eat () {System.out.println ("Cat eats Fish");} Override the Age () method of the parent class public void age () {System.out.println ("5-year-old cat");}}

If we usually call the two methods of cat and dog exhaustion, we need to write this.

Assigning a corresponding method to each class is too tedious to change the way you do it.

public class Test {public static void main (string[] args) {//new An instance object of two classes cat C=new cat ();D og d=new Dog ();//new out of this class object Test no Use Statictest test=new test (); Test.test (c); Test.test (d); Public  void Test (Dog d) {d.age ();d. Eat ();} Public  void Test (Cat c) {c.age (); C.eat ();}}

The parameter type in the original test method is changed to the type of the parent class, and the result is the same.

The compilation type is Dongwu, and the actual type of the runtime becomes a specific type such as the D,D,DW in the example

This does not need to write a method for each type, only need to write a method, flexible and convenient, the program runs according to the specific parameter type to execute the corresponding method

For

Dongwu dw=new Cat (); The new instance object in this way can only invoke methods defined in the parent class, and the newly added method in the subclass cannot be called.
public class Test {public static void main (string[] args) {//new An instance object of two classes cat C=new cat ();D og d=new Dog ();//parent class is DONGWU reference An instance object that points to a subclass (new Cat ()) Dongwu dw=new Cat ();//new out of this class object test does not use Statictest test=new test (); Test.test (c); Test.test (d); Test.test (DW);} Define the parameter type as the type of the parent class public  void Test (Dongwu DW) {dw.age ();d w.eat ();}

  

Java understanding of Polymorphism

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.