Main Content

Calling base class constructors

Archive - Originally posted on "The Horse's Mouth" - 2008-10-03 07:13:33 - Graham Ellis

In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point.

In your code for your base class, you'll have some logic which sets up new objects (a "constructor" method), and you'll have code in your extended classes through which you set up objects of that extended type. Almost inevitably, your extended objects will be pretty similar to your basic objects but they'll have a few extras, and so the writers of Object Oriented languages provide you with a way of calling the base class constructor within (or before) your extended class constructor. The base class is sometimes called the parent class. Let me translate that into an example in plainer English.

"A Train journey is a specialised type of public transport journey. If you're setting up a train journey, you'll want to set up a regular public transport journey within it first, with attributes like where it goes from and to, and at what time and who runs it. Then you will add some train extras such as how many carriages long it is"

How does this work in C++?

Train::Train(int nvh, int vhc, int xtrwa ) : PubT(nvh, vhc) {
 tronly = xtrwa;
 }


A Train is a PubT. When you create a train (with three parameters), you create first a PubT passing the first two parameters in to it, and them you perform the extra actions in the code block - which is saving the third parameter.

Course - C++ Programming

How does this work in Java?

public Train (int nvh, int vhc, int xtrwa) {
 super (nvh,vhc);
 this.tronly = xtrwa;
 }


How does this work in Python?

def __init__(self,nvh,vhc,xtrwa):
   pubt.__init__(self,nvh,vhc)
   self.tronly = xtrwa


Course - Python Programming

This is old style classes; in new style classes, you'll call parent on the current class to avoid having to state the name of the parent class within the extended class definition.

How does this work in Perl?

In Perl, you "roll you own" ... it's so flexible and there are so many options it's almost untrue! Here's an example:

sub new {
 my ($self,$nvh,$vhc,$xtrwa) = @_;
 my $cc = new pubt($nvh,$vhc);
 $cc->{"tronly"} = $xtrwa;
 bless \%{$cc};
 }


Course - Perl for Larger Projects

How does this work in [incr-Tcl]?

constructor {nvh vhc xtrwa} {
    pubt::constructor $nvh $vhc } {
    set tronly $xtrwa
    }
  }


Course - Tcl Basics and please let us know when you book that you would like us to add in strong coverage of Incr-Tcl

How does this work in PHP?

public function __construct($nvh, $vhc, $xtrwa) {
  parent::__construct($nvh, $vhc);
  $this->tronly = $xtrwa;
  }


This example is for PHP 5. If you're still using PHP version 4, you'll call your constructor the same name as the name of the class, and call the base class constructor via the class name.

Course - Object Oriented PHP

def initialize(nvh, vhc, xtrwa)
  super(nvh, vhc)
  @tronly = xtrwa
  end


How does this work in Lua?

Lua is a small language which provides you with facilities through which you can write OO like code but it is not strictly objects. You can set the metatable of a table [object] to the metatable of the table which you wish to be the parent, then modify it - thus emulating the facility that I'm talking about in this article.

Course - Lua Programming