Example 1
The parent class forces the child class to rotate
Father f = new Father(); Son s = (Father)f;//出错 ClassCastException
Analysis:
Create an instance of the parent class and want to force Converting parent class objects into subclasses is not possible! In layman's terms, a real father can never pretend to be a son.
Online learning video tutorial sharing: java online tutorial
Example 2
"Fake" parent class forced to rotate subclass
Father f = new Son(); Son s = (Son)f;//可以
Analysis:
The parent class object refers to a subclass instance.
The properties unique to the Son class cannot be operated by f for the time being, because the Father class does not have the unique properties of the Son class (subclass).
Then create the subclass object s, which refers to the object that was forcibly converted from the parent class object f (actually it is a pretending Son, who was forcibly converted back to Son). At this time, you can pass s To operate the unique attributes of the subclass.
In layman's terms, a son is pretending to be his father. He is still a son after all, and his essence has not changed. He can still be forced to turn back into a son.
Example 3
Forced subclass conversion to parent class
Son s = new Son(); Father f = (Father)s;//可以
Analysis:
The subclass is converted to the parent class, but the unique attributes of the subclass object cannot be Using the f operation, f can operate on its non-specific properties (properties inherited from the parent class).
In layman’s terms, what a son and his father have in common is that they are both human beings. A son is a human being and inherits it from his father. They both have basic human behaviors, but a son can never be the same generation as his father. (Subclass type is converted to parent class type).
Recommended related articles and tutorials: java quick start
The above is the detailed content of Conversion problem between parent class and subclass in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
