Dark Horse programmer _ object-oriented --- & gt; polymorphism (on)

Source: Internet
Author: User

----------- Android training, java training, and java learning technology blog. We look forward to communicating with you! ------------
Polymorphism: it can be understood as multiple manifestations of the existence of things.
1. embodiment of Polymorphism
The reference of the parent class points to its own subclass object.
The reference of the parent class can also receive its own subclass object.
2. The premise of Polymorphism
It must be related to a class, either inheritance or implementation.
There is usually another premise: coverage exists.
3. benefits and disadvantages of Polymorphism
Advantage: the appearance of polymorphism greatly improves the scalability of the program.
Disadvantage: It improves scalability, but can only access members in the parent class by referencing the parent class.
Code Demonstration:
Animals: cats and dogs
[Java]
Abstract class Animal
{Www.2cto.com
Public abstract void eat ();
}
Class Cat extends Animal
{
Public void eat ()
{
System. out. println ("eat fish ");
}
Public void catchMoust ()
{
System. out. println ("catch mouse ");
}
}
Class Dog extends Animal
{
Public void eat ()
{
System. out. println ("eat bone ");
}
Public void lookHome ()
{
System. out. println ("look home ");
}
}
Class Pig extends Animal
{
Public void eat ()
{
System. out. println ("si liao ");
}
Public void gongDi ()
{
System. out. println ("gong di ");
}
}
Class DuoTaiDemo
{
Public static void main (String [] args)
{
/*
Cat c1 = new Cat ();
Cat c2 = new Cat ();
Pig p = new Pig ();
Function (c1 );
Function (new Dog ());
Function (p );
*/
// Animal c = new Cat ();
// C. eat ();
// Function (new Cat ());
// Function (new Dog ());
// Function (new Pig ());
Animal a = new Cat (); // type improvement, upward Transformation
A. eat ();
// What if I want to call a unique method of the cat?
// Forcibly convert the reference of the parent class to the subclass type.
Cat c = (Cat);
C. catchMouse ();
// Do not convert the parent class object to a subclass type.
// What we can convert is that when the parent class references its own subclass object, the application can be upgraded or forced to convert.
// Polymorphism is always a subclass object from beginning to end.
// Animal a = new Animal ();
// Cat c = (Cat) a; ----> is error
}

Public static void function (Animal)
{
A. eat ();
If (a instanceof Cat)
{
Cat c = (Cat);
C. catchMouse ();
} Else if (a instanceof Dog)
{
Dog d = (Dog);
D. lookHome ();
}
 
}
/*
Public static void function (Cat c)
{
C. eat ();
}
Public static void function (Dog d)
{
D. eat ();
}
Public static void function (Pig p)
{
P. eat ();
}
*/
}

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.