Error prone point of Java knowledge: object transformation

1. Object transformation concept Type conversion (automatic conversion and coercion) can be performed between basic data...
Point 1
Point 2
Point 3
Point 4

1. Object transformation concept

Type conversion (automatic conversion and coercion) can be performed between basic data type variables of different types, and object types with inheritance relationship can also be converted in Java. Java allows reference variables of the parent type to directly reference objects of the child type.

2. Transformation objects

Suppose that class A is the parent class of class B. when an object is created with a subclass and the reference of this object is placed in the object of the parent class, object a is called the upper transformation object of object B.

A a = new B();
3. Use summary

Point 1

  • The upper transformation object cannot access the data field added by the subclass; You cannot directly access the methods added by the subclass (overwriting and hiding methods defined in the subclass are not added). Only when the object type is cast to a subclass type can the corresponding call be made
class Monkey{ void crySpeak(String s) { System.out.println(s); } void crawl(){ System.out.println("crawling"); } } class People extends Monkey { int d =0; //New data field of subclass void computer(int a,int b) { //Method for adding subclasses int c=a*b; System.out.println(c); } @Override void crySpeak(String s) { System.out.println("***"+s+"***"); } } public class Example5_10 { public static void main(String args[]) { Monkey monkey = new People(); monkey.crySpeak("I love this game"); //Call subclasses. Rewriting is not new monkey.crawl(); //monkey.computer(10,10); // After the transformation, the new methods of subclasses are lost //System.out.println(monkey.d);// After the transformation, the new variables in the subclass are lost People people = null; if (monkey instanceof People) people=(People)monkey; //Force the upper transformation object into a subclass object people.computer(10,10); System.out.println(people.d); } }
***I love this game*** crawling 100 0

In the above example, the parent class is Monkey and the child class is People. In the subclass, int d is a new variable and computer is a new method. Therefore, the upper transformation object cannot directly call these two variables. It cannot be called normally until it is cast to a subclass type.

Point 2

  • The upper transformation object can access the data field, method inherited from the parent class by the subclass, or the instance method overridden by the parent class in the subclass, but cannot directly access the static method overridden by the parent class hidden in the subclass and the data field defined by the parent class hidden in the subclass.
  • If a subclass overrides an instance method of the parent class, when the instance method is called with the transformation object, the instance method in the subclass must be called.
  • If the subclass hides a static method of the parent class, when the static method is called with the transformation object, the static method in the parent class must be called instead of the static method in the subclass. If the output value is a static variable, it should also be a static variable in the parent class.
public class MethodOverride { public static void main(String[] args) { Child c=new Child(); System.out.println(c.getClassName()); System.out.println("----------------"); Parent p=new Parent(); System.out.println(p.getClassName()); System.out.println("----------------"); Parent pc=new Child(); System.out.println(pc.getClassName()); System.out.println(pc.getX()); System.out.println(pc.getZ()); } } class Parent{ int x =5; static int z = 10; String getClassName(){ return "this is Parent!"; } int getX(){ return x; } static int getZ(){ return z; } } class Child extends Parent{ int x =6; int y =7; static int z =11; String getClassName(){ System.out.println(x+" "+y); return "this is Child!"; } int getX(){ return x; } static int getZ(){ return z; } }
6 7 this is Child! ---------------- this is Parent! ---------------- 6 7 this is Child! 6 10 Process finished with exit code 0

In the above MethodOverride file, the Parent class is Parent and the Child class is Child. The first two objects are instantiations of specific classes, so they call the instance methods of their own classes.
The third is the upper transformation object. getClassName() is an overriding method of the parent class. Therefore, it calls the getClassName() of the child class. X and Y in the method are indirect access to the data field and the of the child class. getX() also makes sense.
getZ() is a static method. Although subclasses hide static variables and static methods, they cannot be accessed directly. According to the principle of looking at the left side of the static call, the call should be the getZ() of the parent class, and the z value is also the data field of the parent class.

Point 3

  • If the method inherited by the subclass from the parent class is not overwritten or hidden, if there is a member variable call in this method, this call is for the member variable of the parent class.
package com.codeslogan.inherit; public class HidingDemo { public static void main(String[] args) { A x = new B(); System.out.println("(1) x.i is " + x.i); System.out.println("(2) (B)x.i is " + ((B)x).i); System.out.println("(3) x.j is " + x.j); System.out.println("(4) ((B)x).j is " + ((B)x).j); System.out.println("(5) x.m1() is " + x.m1()); System.out.println("(6) ((B)x).m1() is " + ((B)x).m1()); System.out.println("(7) x.m2() is " + x.m2()); System.out.println("(8) x.m3() is " + x.m3()); System.out.println("(9) x.z is " + x.z); System.out.println("(10) x.z is " + x.w); } } class A { public int i = 1; public static int j = 11; public int z = 3; public static int w = 13; public static String m1() { return "A's static m1"; } public String m2() { return "A's instance m2"; } public String m3() { return "A's instance m3" + j; } } class B extends A { public int i = 2; public static int j = 12; public static String m1() { return "B's static m1"; } public String m2() { return "B's instance m2"; } }
(1) x.i is 1 //Viewing the parent class from the instance data field of the upper transformation object (2) (B)x.i is 2 //Look at subclasses after strong transformation (3) x.j is 11 //Viewing the parent class from the static data field of the upper transformation object (4) ((B)x).j is 12 //Look at subclasses after strong transformation (5) x.m1() is A's static m1 //Viewing parent class from static method (left) (6) ((B)x).m1() is B's static m1 //Look at subclasses after strong transformation (7) x.m2() is B's instance m2 //Instance method override, look at subclasses (8) x.m3() is A's instance m3 11 //The subclass of the inherited method does not have its corresponding override. The result depends on the parent class //If a data field is involved, whether static data or instance data, it should be the data field of the parent class (9) x.z is 3 //Obviously, the inherited instance data (10) x.z is 13 //Inherited static data

Summary:

  • Inherited (the subclass does not perform additional operations on it), all depends on the content of the parent class
  • After the forced conversion, read the content after the forced conversion unconditionally
  • The data field looks at the parent class by default
  • If the upper transformation object is called and the method of the parent class is called, the corresponding data should also be the data of the parent class

Point 4

  • Even if the upper transformation object uses the parent class for a cast, the accessed overridden instance method is still a subclass
A a = new B(); a.getX(); ((A)a).getX(); //a.getX();

In short, it turns out that its type is A, and turning to A is equivalent to not turning.

package com.codeslogan.inherit; public class TestChange { public static void main(String[] args) { Animal x=new Tiger(); System.out.println("(1):x.news is "+x.news); System.out.println("(2):((Tiger)x).news is "+((Tiger)x).news); System.out.println("(3):x.smile() is "+x.smile()); System.out.println("(4):((Tiger)x).smile() is "+((Tiger)x).smile()); System.out.println("(5):x.getNews() is "+((Animal)x).getNews()); System.out.println("(6):x.getNews() is "+x.getNews()); System.out.println("(7):x.getMessage() is "+x.getMessage()); System.out.println("(8):((Tiger)x).eat() is "+((Tiger)x).eat()); } } class Animal{ public String news="Animal's news"; public String message="Animal's message"; public static String smile(){ return "smile from Animal"; } public String getNews(){ return news; } public String getMessage(){ return message; } } class Tiger extends Animal{ public String news="Tiger's news"; public String message="Tiger's message"; public static String smile(){ //hide return "smile from Tiger"; } public String getNews(){ //cover return news; } //New method public String eat(){ //new return "need eat meat"; } }
(1):x.news is Animal's news (2):((Tiger)x).news is Tiger's news (3):x.smile() is smile from Animal (4):((Tiger)x).smile() is smile from Tiger (5):x.getNews() is Tiger's news (6):x.getNews() is Tiger's news (7):x.getMessage() is Animal's message (8):((Tiger)x).eat() is need eat meat

After the above precipitation, there should be no problem here. Leave a message below the problem

4. * construction method

This section discusses the construction method of the upper transformation object and the situation when the construction method calls the instance method, as follows:

class A { int value = 5; A( ) { setValue(10); System.out.println( "A:" + value); } void setValue(int v) { value = 2 * v; } } class B extends A { B( ) { //super(); System.out.println( "B:" + value); } @Override void setValue(int v) { value = 5 * v; } } public class TestC { public static void main(String[ ] args){ new A( ); new B( ); A c =new B(); } }

The running answer is as follows:

A:20 ------------- A:50 B:50 ------------- A:50 B:50

I was a little surprised when I just touched this question. I didn't expect that Java could be tested like this. I feel that the Java I usually learn and the Java taught by my teacher are not the same thing, but I still have to bear it silently (40000!!!) I don't say much and get to the point

The first value output is 20. The parent object is created, and its construction method + instance method is called. There is no need to say more about the result
The second group consists of two 50. When creating a B object, because it is a subclass of class A, the constructor of a will be called first by default. But in A's construction method, an instance method is called. According to the previous knowledge, if the subclass overrides the instance method of the parent class, the method of the subclass should be called first, so the value value is 50.
The answers of the third group are the same as those of the second group. The reason is the same as above. As a result, the transformation is just a cover up.

reference material

A Haida teacher surnamed Zhang

2 October 2021, 19:03 | Views: 7397

Add new comment

For adding a comment, please log in
or create account

0 comments