1

How is it that the Fly class was able to receive the instance variable 'x' in super class if:

  1. The super() function is not called in the Fly class constructor
  2. 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.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
Max
  • 21
  • 4
  • Second version of Super is not valid, please provide the right one – Jean-Baptiste Yunès Mar 03 '21 at 16:48
  • Neither `this()` nor `super()` are functions, they are a sibling constructor or a parent constructor, respectively. – Mark Rotteveel Mar 03 '21 at 17:02
  • Related: [Why call super() in a constructor?](https://stackoverflow.com/questions/10508107/why-call-super-in-a-constructor), [Is it unnecessary to put super() in constructor?](https://stackoverflow.com/questions/2054022/is-it-unnecessary-to-put-super-in-constructor) – Mark Rotteveel Mar 03 '21 at 17:10
  • @Jean-BaptisteYunès the second version of super is what I thought I would be. – Max Mar 03 '21 at 19:26
  • @Jean-BaptisteYunès Why is the second version not valid? I put that there because that is what I would assume to be correct, but I guess i'm mistaken. – Max Mar 03 '21 at 19:30

2 Answers2

1

If you dont call explicitly a super constructor then a call to a super constructor with no argument is emitted (i.e. super() calls Super()).

If you dont explicitly initialise a field, it is initialised with a default value (0 for numbers, false for booleans and null for references).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

In Java, the constructor of child classes always have an implicit super() in the first line of the constructor.

In addition to that, x is always being initialized by the super class, as it is initialized during declaration and not in the constructor. And, even if you simply leave the member declared as protected int x; it will be assigned a default value (0).