Learn java--polymorphism from the beginning

Source: Internet
Author: User

I. The definition of polymorphism

What is polymorphism? Polymorphism can be understood as the various manifestations of the existence of things. The same thing, under different conditions, shows a different result.

For example, there is a cat, and a dog, collectively known as an animal. When we have a cat in front of us, we say animals, that is the cat, if we are in front of a dog, then we say that animals are referring to the dog.

How polymorphic is defined: the reference = new subclass of the parent class parent class. A reference to the parent class can also accept the child class object.

We can greatly improve the extensibility of the program by invoking the method of the subclass according to the reference of the parent class.

For example:

Abstract class Animal{public abstract void Eat ();} Class Cat extends Animal{public void eat () {System.out.println ("Eat cat Food");}} Class Dog extends Animal{public void eat () {System.out.println ("Eat dog Food");}} public class Polymorphismtest {public static void main (string[] args) {Animal animal1 = new Cat (); Animal animal2 = new Dog (); Feedanimal (ANIMAL1); Feedanimal (ANIMAL2);} static method, feed animal, receive Animal type, execute subclass method public static void Feedanimal (Animal Animal) {animal.eat ();}}

If there is no polymorphism, then the static method of feeding animals needs to write various eat methods according to various animals. If there are new animals, then the changes will be very large. With polymorphism, we only need to invoke the Eat method of various animals according to the reference of the parent class animal.


1. polymorphic Embodiment: The parent class refers to an object that points to a child class. Can be declared when defined, or acceptable.

2. Polymorphism premise: (a) there is an inheritance or implementation of the relationship between classes (b) the existence of methods of coverage

3. Increased extensibility, but note that the parent class uses a reference to the method that exists in the parent class, and the subclass is implemented.

two. polymorphic TransformationA reference to a parent class type refers to an object of a subclass, but the subclass object has its own methods and fields, and if you want to call the subclass's own method, the parent class does not have these methods. The answer is that the parent class reference is cast to the subclass reference, but the object is still an object of the subclass and has not changed.
The parent class class Parent{//eat method public void Eat () {System.out.println ("parent Eat");}} Subclass, inheriting the parent class class son extends parent{//subclass eat method, overwrite the parent class eat method public void Eat () {System.out.println ("Son eat");} public void Play () {System.out.println ("Son play");} Mainpublic class PolymorphismTest2 {public static void main (string[] args) {Parent parent = new Son ();//Call Eat method Parent.ea t ();//Call the Play method, but the parent class does not have a play method, so convert the reference down to the Son type and have the play method to invoke ((Son) parent). Play ();

Key points: 1. When you define a subclass object and use the parent class reference (or assign the subclass object directly to the parent class reference), it is called upward transformation and can be transformed directly. 2. Moving from a parent class reference to a subclass reference, called down transformation, cannot be directly transformed and requires casting. 3. Note that the object is not converted from start to finish, the conversion is a reference!!!
Three. Polymorphism characteristics
public class Parentchildtest {public static void main (string[] args) {parent parent=new parent ();//100parent.printvalue ( ); Child child=new Child ();//200child.printvalue ();p arent=child;parent.printvalue ();//200parent.myvalue++; Parent.printvalue ();//200 ((child) parent). myvalue++;p Arent.printvalue ();//201}}class parent{public int myvalue=100 ;p ublic void Printvalue () {System.out.println ("Parent.printvalue (), myvalue=" +myvalue);}} Class Child extends Parent{public int myvalue=200;public void Printvalue () {System.out.println ("Child.printvalue (), Myvalue= "+myvalue);}}

Key points: 1. When a variable of the same name is found in the parent and child classes in polymorphism, a variable of the reference type is used, depending on the variable of the reference type, the child class or the variable in the parent class. 2. When there is a method with the same name in the parent class and subclass in polymorphism, the object type is viewed. The object is a subclass, a method that calls a subclass, the object is the parent class, and the method of the parent class is called. 3. When the parent and child classes in polymorphic have the same static, they see that they are reference types.

Learn java--polymorphism from the beginning

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.