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.e. i = 60) with the help of a Child class object. How can I do that?
System.out.println(((Parent) c).i);
The simplified template for casting an EXPRESSION or a VARIABLE to a TYPE is:
((TYPE)(EXPRESSION|VARIABLE))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With