Java super keyword

Call Superclass Constructors via *super*.

A subclass can call a constructor defined by its superclass by use of the following form of super:

super(arg-list); 
// LegoBlockWeight now uses super to initialize its LegoBlock attributes.
class LegoBlockWeight extends LegoBlock {
  double weight; // weight of box

  // initialize width, height, and depth using super()
  LegoBlockWeight(double w, double h, double d, double m) {
    super(w, h, d); // call superclass constructor
    weight = m;/*from www.ja va  2  s .c o  m*/
  }    
}

Full source code

 
 // A complete implementation of LegoBlockWeight.
class LegoBlock {
  private double width;
  private double height;
  private double depth;

  // construct clone of an object
  LegoBlock(LegoBlock ob) { // pass object to constructor
    width = ob.width;/*ww  w .  j  a  va2 s.  com*/
    height = ob.height;
    depth = ob.depth;
  }

  // constructor used when all dimensions specified
  LegoBlock(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
  }

  // constructor used when no dimensions specified
  LegoBlock() {
    width = -1;  // use -1 to indicate
    height = -1; // an uninitialized
    depth = -1;  // box
  }

  // constructor used when cube is created
  LegoBlock(double len) {
    width = height = depth = len;
  }

  // compute and return volume
  double volume() {
    return width * height * depth;
  }
}

// LegoBlockWeight now fully implements all constructors.
class LegoBlockWeight extends LegoBlock {
  double weight; // weight of box

  // construct clone of an object
  LegoBlockWeight(LegoBlockWeight ob) { // pass object to constructor
    super(ob);
    weight = ob.weight;
  }

  // constructor when all parameters are specified
  LegoBlockWeight(double w, double h, double d, double m) {
    super(w, h, d); // call superclass constructor
    weight = m;
  }    

  // default constructor
  LegoBlockWeight() {
    super();
    weight = -1;
  }

  // constructor used when cube is created
  LegoBlockWeight(double len, double m) {
    super(len);
    weight = m;
  }
}
  
public class Main {
  public static void main(String args[]) {
    LegoBlockWeight myLegoBlock1 = new LegoBlockWeight(10, 20, 15, 34.3);
    LegoBlockWeight myLegoBlock2 = new LegoBlockWeight(2, 3, 4, 0.076);
    LegoBlockWeight myLegoBlock3 = new LegoBlockWeight(); // default
    LegoBlockWeight mycube = new LegoBlockWeight(3, 2);
    LegoBlockWeight myclone = new LegoBlockWeight(myLegoBlock1);
    double vol;

    vol = myLegoBlock1.volume();
    System.out.println("Volume of myLegoBlock1 is " + vol);
    System.out.println("Weight of myLegoBlock1 is " + myLegoBlock1.weight);
    System.out.println();

    vol = myLegoBlock2.volume();
    System.out.println("Volume of myLegoBlock2 is " + vol);
    System.out.println("Weight of myLegoBlock2 is " + myLegoBlock2.weight);
    System.out.println();

    vol = myLegoBlock3.volume();
    System.out.println("Volume of myLegoBlock3 is " + vol);
    System.out.println("Weight of myLegoBlock3 is " + myLegoBlock3.weight);
    System.out.println();
 
    vol = myclone.volume();
    System.out.println("Volume of myclone is " + vol);
    System.out.println("Weight of myclone is " + myclone.weight);
    System.out.println();

    vol = mycube.volume();
    System.out.println("Volume of mycube is " + vol);
    System.out.println("Weight of mycube is " + mycube.weight);
    System.out.println();
  }
}

Reference field from super class via super keyword

This usage has the following general form:

super.member 

// Using super to overcome name hiding.
class A {/* w w w .  j ava  2  s.  c  om*/
  int i;
}

// Create a subclass by extending class A.
class B extends A {
  int i; // this i hides the i in A

  B(int a, int b) {
    super.i = a; // i in A
    i = b; // i in B
  }

  void show() {
    System.out.println("i in superclass: " + super.i);
    System.out.println("i in subclass: " + i);
  }
}
  
public class Main {
  public static void main(String args[]) {
    B subOb = new B(1, 2);

    subOb.show();
  }
}



PreviousNext

Related