Using java to convert children to parent problems

class Parent {
class Parent {
    int i = 60;
}

class Child extends Parent {
    int i = 70;
}

class Main {

    public static void main(String[] args) {
        Child c = new Child();
        Parent p = new Parent();

        System.out.println((Parent c).i);
    }

}

Here, I want to print a parent class variable (I = 60) with the help of the child class object How can I do this?

Solution

System.out.println(((Parent) c).i);
System.out.println(((Parent) c).i);

The simplified templates for converting expression or variable to type are:

((TYPE)(EXPRESSION|VARIABLE))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>