Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Super Constructors

Part of inheritance is defining the hierarchy of object instantiation. The super() method on a class constructor allows a subclass to pass arguments and execute the constructor of its superclass. The super() method is accessed using a semicolon delimiter off the constructor method, like this:

Constructor():super()

Super constructors give the subclass the flexibility to custom-tailor constructor parameters and initialize its own class fields. The parameters are passed from the outside in, with the top-most class in the hierarchy being instantiated first.

Implicit super

If no parameters are defined in a superclass constructor, you can bypass the call to :super() in your subclass. The superclass constructor in Example 4.20 will be called implicitly.

EXAMPLE 4.20

abstract class Vertebrate extends Object
{
  Vertebrate()
  {
    print('Vertebrate is: Spined');
  }
}

class Cat extends Vertebrate
{
  Cat () //an implicit call to :super() occurs prior to executing constructor
  {
    print("Cat Is: Alive");
  }
}

main()
{
  Cat pet = new Cat ();
}

//Output:
//Vertebrate is: Spined
//Cat Is: Alive

Upon Cat instantiation, the superclass constructor for Vertebrate is implicitly executed. Upon the completion of the Vertebrate constructor, control is passed back to the descendant class Cat constructor.

Explicit super()

If your constructors define parameters, you must make a call to :super() via the subclass constructor and provide the requested arguments (Figure 4.2).

EXAMPLE 4.21

abstract class Vertebrate extends Object
{
 Vertebrate(String action)
  {
    print('Vertebrate is: $action'); //first statement executed
  }
}

abstract class Bird extends Vertebrate
{
  Bird(String action):super('Spined')
  {
    print('Bird is: $action');
  }
}

class Finch extends Bird
{
  String color;
  Finch(this.color):super('Winged')
  {
    print('Finch is: $color');
  }
}

main()
{
    Bird animal = new Finch("Yellow");
}

//Output:
//Vertebrate is: Spined
//Bird is: Winged
//Finch is: Yellow
FIGURE 4.2

FIGURE 4.2 Inheritance chain and execution order

In Example 4.21, you see the instantiation of a Finch object with an argument of Yellow. The argument Yellow will eventually be assigned to the field color inside of class Finch.

Assignment of field color will occur after the classes higher in the inheritance chain have finished instantiation. The statement :super('Winged') is interpreted prior to the instantiation of class Finch. The statement :super('Spined') is interpreted prior to the instantiation of class Bird.

Once you reach the top-most class in the hierarchy, the Vertebrate constructor statement is executed. Upon the completion of the Vertebrate constructor, control is passed back to the descendant class constructors in a “last in, first out” order.

  • + Share This
  • 🔖 Save To Your Account