search
HomeJavaJavaBaseConversion problem between parent class and subclass in java

Conversion problem between parent class and subclass in java

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment