Casting between parent and child classes in Java

Source: Internet
Author: User

The next three points are from Baidu I know on the quoted. Hehe ~, feel good.

1. Understand from the memory angle of the object.
Suppose that there is now a parent class father, the variables inside it need to occupy 1 m of memory. There is a subclass of it that has a variable that takes up 0.5M of memory.
Now let's look at the allocation of memory by code:
2.F = new Father ();//The system allocates 1 m of memory.
Son s = new Son ();//The system will allocate 1.5M of memory! Because a hidden reference in a subclass super points to the parent class instance, a parent class is instantiated before the subclass is instantiated, that is, the constructor of the parent class is executed first. Because s contains an instance of the parent class, So s can call the method of the parent class.
3.Son S1 = s;//s1 points to that 1.5M of memory.
Father f1 = (Father) s;//then F1 points to the 1.5M memory in 1 m of memory, that is, F1 only point to the instance of the parent class instance object, so F1 can only call the method of the parent class (stored in 1 m memory), and cannot call the method of the subclass (stored in 0.5M memory ).
Son s2 = (son) f;//This code runs as a classcastexception. Because there is only 1 m of memory in F, references to subclasses must have 1.5M of memory, so they cannot be converted.
Son s3 = (son) f1;//This sentence can be run, then S3 point to that 1.5M of memory. Since F1 is converted by S, it has 1.5M of memory, but it only points to 1 m of memory.

Then I read the idea myself:

1, this sentence is very important: in the subclass constructor, the subclass will hide the reference super point to the parent class instance, so before instantiating the subclass, the parent class's constructor is executed first. The subclass object contains an instance of the parent class.

2, when forced conversions, subclasses can convert to the parent class, because they can be chopped off (ignored). Some, however, the parent cannot convert to a subclass, and you cannot make a forced conversion to make it add some memory.

If there is a mistake welcome comment!!!

Casting between parent and child classes in Java

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.