[Java Study Notes] On-going transformation and on-going transformation

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

See the program section below

Package test;
Class {
Public String f (a OBJ)
{
Return ("");
}
}
Class B extends {
Public String F (B OBJ)
{
Return ("C ");
}
Public String f (a OBJ)
{
Return ("D ");
}
}

We use subclass to create an object:

B = new B ();

Then, place the reference of this subclass to the parent class object:

A;

A = B;

Then this parent class object a is the transformation object of the subclass object B. The object entity is created by the subclass. In essence, it is still a subclass, but it only loses some features. The gains and losses are as follows:

The transformation object can operate and use methods inherited or rewritten by subclass.

Loss: the transformation object loses the operation and use of new member variables or new methods in the subclass.

 

The following test code is printed as "D", because A2 can only use the Public String f (a OBJ) method.

Public class testobj
{
Public static void main (string ARGs [])
{A a1 = new ();
A a2 = new B (); // The subclass object is converted to the parent class, which is called the upper transformation and does not need to be forcibly converted.

B = new B ();
System. Out. println (a2.f (B); // "D"

System. Out. println ();

 

If (A2 instanceof B ){
B b1 = (B) A2; // The parent class object is converted to the next transformation. In this case, use forced conversion, in this case, you must first determine whether the object to be converted (that is, the object directed by A2) is a B-class instance.
System. Out. println (b1.f (B); // "C"
System. Out. println (b1.f (A1); // "D"
}
}
}

 

In fact, the Java transformation problem is not complicated. Just remember one sentence: the parent class reference points to the subclass object. When a subclass defines a variable with the same name as its parent class, the variable is hidden and there is no polymorphism. For the overwriting method, Java shows polymorphism and calls the method in a more specific subclass, no matter where the call is from, no matter what reference type call is used.


In addition, let's talk about the role of upward Transformation: due to the role of upward transformation, we can easily define the parent class instance as a parameter in the input parameter of the method when we want to pass in the subclass instance, this eliminates the need to use the polymorphism feature for method overloading. In fact, upward transformation is also a manifestation of polymorphism.

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.