The Java transformation problem is not complicated, just remember the word: The parent reference points to the subclass object.
What is a parent class reference to a subclass object, and listen to me slowly.
Start with 2 nouns: upward transformation (upcasting), downward transformation (downcasting).
For example: There are 2 classes, Father is the parent class, and the son class inherits from father.
Father f1 = new Son (); This is called upcasting (upward transformation)
Now the F1 reference points to a son object
son S1 = (son) F1; This is called downcasting (downward transformation)
Now F1 still point to son object
A 2nd example:
Father F2 = new Father ();
Son s2 = (son) F2; Error, subclass reference cannot point to parent class object
You might ask, in the 1th example: son s1 = (son) F1; ask what is right.
Very simple because F1 points to a subclass object, Father f1 = new Son (); The subclass S1 Reference can of course point to the child class object.
The F2 is passed to a Father object, Father f2 = new Father (); The subclass S1 reference cannot point to the parent class object.
Summarize:
1. The parent class reference points to the child class object, and the subclass reference cannot point to the parent class object.
2. The target class object is directly assigned to the parent class reference called upcasting, which transforms upward without casting.
such as: Father F1 = new Son ();
3. Assign the parent class reference to the subclass object to the subclass reference called down Transformation (downcasting), to cast.
For example, F1 is a parent class reference to a subclass object. Assign F1 to the subclass reference S1 that son S1 = (son) F1;
Where the F1 front (Son) must be added, cast.
First, the upward transformation.
In layman's words, a subclass object is converted to a parent class object. The parent class object can be an interface here.
1, the method call in the upward transformation.
Look at the following code:
[Java]View Plaincopyprint?
- Package com.wensefu.others;
- public class Animal {
- public void Eat () {
- System.out.println ("Animal eatting ...");
- }
- }
- Class Bird extends animal{
- public void Eat () {
- System.out.println ("Bird eatting ...");
- }
- public void Fly () {
- System.out.println ("Bird flying ...");
- }
- }
- Class main{
- public static void Main (string[] args) {
- Animal b=new Bird (); Upward transformation
- B.eat ();
- //! Error:b.fly (); B points to the subclass object, but loses the fly () method at this point
- Dosleep (New Male ());
- Dosleep (New Female ());
- }
- public static void Dosleep (Human h) {
- H.sleep ();
- }
- }
Package com.wensefu.others;
- public class Human {
- public void sleep () {
- System.out.println ("Human sleep.");
- }
- }
- Class Male extends Human {
- @Override
- public void sleep () {
- System.out.println ("Male sleep.");
- }
- }
- Class Female extends Human {
- @Override
- public void sleep () {
- System.out.println ("Female sleep.");
- }
- }
Notice the upward transformation here:
Animal b=new Bird (); Upward transformation
B.eat ();
The Eat () method of the subclass is called here. Cause: B is actually pointing to the bird subclass, so it calls the method of the subclass itself.
It is important to note that when you move up, B loses other methods that are common to the parent class object. As in this example, the Fly method is no longer owned by B.
2, the benefits of upward transformation.
Look at the code above,
public static void Dosleep (Human h) {
H.sleep ();
}
In this case, the parent class is the parameter, and sometimes the sub-class is used as the parameter, which is the use of upward transformation. This makes the code concise. Otherwise
If Dosleep takes a subclass object as a parameter, then how many subclasses will need to write the number of functions. This also embodies the idea of Java abstract programming.
Second, the downward transformation.
In contrast to the upward transformation, the parent object is converted to a subclass object.
Look at the following code:
Package com.wensefu.other1;
- public class Girl {
- public void Smile () {
- System.out.println ("Girl Smile () ...");
- }
- }
- Class Mmgirl extends girl{
- @Override
- public void Smile () {
- SYSTEM.OUT.PRINTLN ("Mmirl smile sounds sweet ...");
- }
- public void C () {
- System.out.println ("Mmirl C () ...");
- }
- }
- Class main{
- public static void Main (string[] args) {
- Girl g1=new Mmgirl (); Upward transformation
- G1.smile ();
- Mmgirl mmg= (mmgirl) G1; Down transformation, compile and run without errors
- Mmg.smile ();
- MMG.C ();
- Girl g2=new Girl ();
- Mmgirl mmg1= (mmgirl) G2; Unsafe down-conversion, compile error-free but run errors
- Mmg1.smile ();
- Mmg1.c ();
- if (G2 instanceof Mmgirl) {
- Mmgirl mmg1= (mmgirl) G2;
- Mmg1.smile ();
- Mmg1.c ();
- }
- }
- }
Girl g1=new Mmgirl (); Upward transformation
G1.smile ();
Mmgirl mmg= (mmgirl) G1; Down transformation, compile and run without errors
The downward transformation here is safe. Because G1 points to a subclass object.
and
Girl g2=new Girl ();
Mmgirl mmg1= (mmgirl) G2; Unsafe down-conversion, compile error-free but run errors
Error running:
Exception in thread "main" Java.lang.ClassCastException:com.wensefu.other1.Girl
At Com.wensefu.other1.Main.main (girl.java:36)
As the code shows, you can prevent exceptions by instanceof.
Java Transformation Issues