The realization of Java polymorphism is mainly embodied in which aspects _java

Source: Internet
Author: User
Tags abstract

Polymorphism in the thinking in JAVA3

People are often confused by, non-object-oriented features to Java, like method overloading, which are sometimes pre sented as object-oriented. Don ' t be fooled:if it isn ' t late binding, it isn ' t polymorphism

Translation by text

People are always confused by another object-oriented feature of Java, like method overloading, sometimes it exists as an object-oriented, so don't be silly, and if there is no late binding (i.e. dynamic binding of the runtime), it is not polymorphic.

So, the meaning of this passage is to say, polymorphic to have dynamic binding, otherwise it is not polymorphic, method overload is not polymorphic (because the method overload is a compile-time decision, no later is the runtime of dynamic binding)

when these three conditions are met 1. There is an inheritance of 2. There are rewrite 3. To have a parent class reference to a child class object

 <span style= "FONT-SIZE:14PX;" > Example: public class Address {private string name, public address (String name) {this.name = name;} public string Getna

Me () {return name.} public void SetName (String name) {this.name = name;}}

Define base class (abstract Class): Public abstract class Vehicle {abstract void Go (address);} Car for the implementation of the base class: public class car extends vehicle{@Override public void Go (address) {System.out.println (' car to ' + ad
Dress.getname ());  Plane for the implementation of the base class: public class Plane extends vehicle{@Override void go [address] {System.out.println ("Plane to"
+ Address.getname ());
} Driver: public void Drive (Vehicle v) {///polymorphic, the parent reference to the subclass object, the actual subclass of the abstract class Vehicle, or the implementation class, and then the compiler will find the implementation method based on the concrete implementation class. V.go ("Hangzhou (abstract)"); This method is overridden in a specific implementation} Test:public static void Main (string[] args) {Driver d = new Driver (); D.drive (new Plane ());//Actual PLA Ne object, the compiler will find the plane in the d.drive (New Car ()) for the go implementation. is actually a car object, the compiler will find the output of the Go implementation} in Plane: Plane toHangzhou (abstract) car to Hangzhou (abstract) </span> 

Polymorphism : refers to the same things in different states, such as: water. Water can be of three states:

Gases, liquids and solids. Then the polymorphism in Java can also be understood to mean that:

Sets the parent object to be equal to one or more of its child objects,

such as Parent=child;

Polymorphism enables the use of the same class (parent class) to refer to objects of different classes.

and perform the same actions in different ways, depending on the object being referenced.

Multi-state implementations include two ways: overloading and overriding

For example: Animal a = new Tiger (); This is an old topic, hehe ...

The parent class reference points to a subclass object, the animal class contains a eat () method, and the Tiger class inherits from the

Animal class, if the subclass overrides the Eat () method of the parent class, then when called, you can follow the subclass

is essentially a method of the parent class, but after the subclass is overridden, it becomes another way, which is polymorphism.

The above is a small series for everyone to talk about the implementation of Java polymorphism is mainly reflected in what aspects of the entire content, hope to help everyone, a lot of support cloud Habitat Community ~

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.