Hinking in Java notes--8th polymorphic

Source: Internet
Author: User

Polymorphic separations do what and how to do it. Also becomes dynamic bound, late-bound, or run-time bound.

8.1 Further discussion upward transformation

An object can be used as its parent type, called an upward transformation.

A method accepts the parent class as a parameter, all subclasses can call this method, the compiler does not determine at compile time which method to execute, at run time depending on whether the corresponding subclass overrides the method to decide which method to invoke, this greatly reduces the programming.

8.2 Transfer?

8.2.1 Method Call Binding

Associating a method call with a method body is called a binding.

Late binding: Binding according to the type of the object at run time

The compiler does not know the type of the object (because the compiler knows only that it is a parent class and does not know which subclass passed in)

Java has late binding in addition to the static method and the final method.

8.2.2 Produce the right behavior

The compiler only knows that an object is a parent class, and only to run time to know what the object is specifically and to perform the correct behavior.

8.2.3 Scalability

The method of manipulating the base class does not have to undergo any modification!

Parent s=new Son ();

What happened: New a subclass, requesting space for the subclass, but the added portion of the subclass after the upward transformation is not visible (still there!). ), the calling method also takes precedence over the subclass override Method!

8.2.4 defect: "Overwrite" private method

Overriding the private method does not error, but the semantics are not covered, but the new method, the upward transformation is not visible, does not reach the effect of polymorphism.

Therefore, to avoid "overwrite" the private method, it should be a different name!

8.2.5 defects: Domain and Static methods

The domain (member variable) is bound at compile time and is not overwritten.

If the subclass and the parent class have the same domain name, each has its own space, and the domain of the sub-class is not visible after the upward transformation (unless the method that the subclass overrides is called)

In practice should avoid this situation, preferred to try to define all the fields as private, and then do not add to subclass a domain with the same name!

Static methods are also not polymorphic.

Hinking in Java notes--8th polymorphic

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.