Monday, May 16, 2011

Calling base class constructor using super in java

In java, we use super to call a constructor in a parent class. Calling the constructor for the superclass must be the first statement in the body of a constructor. If you are satisfied with the default constructor in the superclass, there is no need to make a call to it because it will be supplied automatically.




//X is super class, with attributes width, height, depth and has constructor for 3 attributes.

class Y extends X {
double weight; // weight of box // initialize width, height, and depth using super()

Y(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;

No comments:

Post a Comment