How is it that the Fly class was able to receive the instance variable 'x' in super class if:
- The super() function is not called in the Fly class constructor
- The 'x' variable is not initialized within the Super class constructor
public class Super {
protected int x = 1;
public Super() {
System.out.print("Super");
}
}
public class Duper extends Super {
protected int y = 2;
public Duper() {
System.out.println(" duper");
}
}
public class Fly extends Super {
private int z, y;
public Fly() {
}
public Fly(int n) {
z = x + y + n;
System.out.println(" fly times " + z);
}
public static void main(String[] args) {
Duper d = new Duper();
int delta = 1;
Fly f = new Fly(delta);
}
}
I thought in order for the Fly class to receive the 'x' variable in the Super class, we must call for it using the super() function within the Fly class constructor. However, in order to do that, we would need x to be a formal parameter as shown below:
public class Super {
protected int x = 1;
public Super(**int x**){
**this.x = x**;
public super(){
x =1;
}
System.out.print("Super");
}
}
public class Fly extends Super {
private int z, y;
public Fly() {
}
public Fly(int n) {
**super(x)**
z = x + y + n;
System.out.println(" fly times " + z);
}
In other words, how is it that the 'x' value in fly is defined in the Fly class constructor if no super(x) was called in the parent class? When I call Fly(), it runs. I would think the 'x' variable in the Fly() constructor would be undefined.