简体   繁体   中英

Calling the super constructor in a subclass?

My code

How do you call the superclass constructor in the subclass to make this code work?

class Ale {

   protected int x = 1;

   public Ale( int xx ) {
      x = xx;
   }
}  

class Bud extends Ale {

   private int y = 2;

   public void display() {
      System.out.println("x = " + x + " y = " + y);             
   }
}

You can call to the super constructor like this,

class Ale {

    protected int x = 1;

    public Ale(int xx) {
        x = xx;
    }
}

class Bud extends Ale {

    Bud() {
        super(75);
    }

    private int y = 2;

    public void display() {
        System.out.println("x = " + x + " y = " + y);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM