Polymorphism, polymorphismjava

Source: Internet
Author: User

Polymorphism, polymorphismjava

Polymorphism definition (Baidu encyclopedia): Polymorphism (Polymorphism) literally means "multiple States ". In object-oriented language, multiple implementation methods of interfaces are polymorphism. Referencing Charlie Calverts's description of Polymorphism

-- Polymorphism is a technique that allows you to set a parent object to be equal to one or more other sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it (from "Delphi4 Programming Technology

"). To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Polymorphism is implemented by Virtual functions in Object Pascal and C ++.

As I learned today, Polymorphism is a parent type reference that can point to sub-objects. ("Subclass is the parent class ")

Interface Animal {
Void shout (); // define the abstract shout () method
}
// Define the Cat class to implement the Animal Interface
Class Cat implements Animal {
// Implement the shout () method
Public void shout (){
System. out. println ("meow ...... ");
}
}
// Define the Dog class to implement the Animal Interface
Class Dog implements Animal {
// Implement the shout () method
Public void shout (){
System. out. println ("Wang ...... ");
}
}
// Define the test class
Public class Example13 {
Public static void main (String [] args ){
Animal an1 = new Cat (); // creates a Cat object, which is referenced by an1, a variable of the Animal type.
Animal an2 = new Dog (); // creates a Dog object, which is referenced by an2.
AnimalShout (an1); // call the animalShout () method and pass an1 as a parameter
AnimalShout (an2); // call the animalShout () method and pass an2 as a parameter
}
// Defines the static animalShout () method and receives an Animal type parameter.
Public static void animalShout (Animal ){
An. shout (); // call the shout () method of the actual Parameter
}
}

 

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.