Getting Started with Java-object transformation

Source: Internet
Author: User

The type conversion of an object is something we often encounter when programming, as is the case with the Java platform. For example, some basic types of data transformation and composite data transformation.

Example

         java language is mainly divided into upward transformation and downward transformation, how to understand and grasp the relationship between the two transformation? First of all, let's give a sample, I believe you will be clear after the reading. &NBSP,
        Let's say we have animals, cats and tigers in our real world, What kind of relationship does all three have? Suppose to think with object-oriented thinking. Tigers are subcategories, and cats are the parents of tigers. Tiger object is the object of cats, here to special note is: The tiger is a cat object, then the cat has the characteristics of Tigers have, (in object-oriented is the same property and behavior), this method is the upward transformation (often called implicit transformation).
        Assuming this is the case, there is no problem with the normal thinking of our people, The compiler will not error when compiling. But in the work we often encounter downward transformation situation. But such a downward transition when the compiler will often error, assuming that the cat is a tiger. Here should be a no proposition (at least for now), in the object-oriented world, the more detailed things it has the more characteristics. And the more abstract things, the less characteristic he has, is simply an abstraction of something common.

To make sure that the compiler does not error when the object is down, and that the detailed object is also in the abstract object. Here, we need to infer from the Java keyword that, instanceof, we can use this operator to infer whether a class implements an interface or a subclass of a class.
        Tigers instanceof Cats

Code

take A look at the section below:

Package test;   Class A {public       String f (a obj)       {           return ("a");       }   }   Class B extends A {public       String f (B obj)       {           return ("C");       }       Public String f (A obj)       {           return ("D");       }   }   

We use subclasses to create an object:
b b = new B ();
The reference to the subclass is then placed in the parent class object:
A;
A = b;
Then the parent object A is the top-transforming object of subclass object B. The entity of the object is responsible for establishing, in essence or subclass, just a loss of functionality, such as the following:
: The upper-transition object can manipulate and use subclasses to inherit or override methods.
Lost : The action and use of the new member variable or new method of the child class was lost on the transformed object.
So the result of the test code below is "D". Since a2 can only have public String f (A obj) This method can be used.
public class Testobj   {public       static void Main (String args[])       {  A a1 = new A ();           A A2 = new B (); Subclass objects are converted to parent classes, called top transitions, and do not need to be transformed.              b b = new B ();           System.out.println (A2.F (b)); "D"               System.out.println ();                  if (A2 instanceof B) {                b B1 = (b) A2;  When the parent object is converted to a lower transformation, a cast is required, at which point the object to be converted (that is, the object to which A2 points) is not an instance of Class B               System.out.println (B1.F (b));//"C"               System.out.println (B1.F (A1));//"D"}}   }  


Summarize

Object transformation enables a reference to a parent class object to point to a subclass object, giving the program a better extensibility: we can define a reference to the parent class in the parameters of a method, and then actually pass the object as a subclass, and then we infer in the method which subclass of the passed subclass object belongs to. Then run the method inside the subclass or call the member variables inside the subclass, so the program's extensibility is better than defining multiple methods individually. It's just that scalability is not as good as it is, and using polymorphism allows the program to be scaled up to its fullest extent.

Getting Started with Java-object transformation

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.