Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Calling base class constructors

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
(written 2008-10-03 07:13:33)

 
Associated topics are indexed under
C233 - C and C based languages - OO in C++ - beyond the basics
C234 - C and C based languages - Further C++ Object Oriented features
J710 - Java - Extending Classes and More
Y112 - Python - Objects - Intermediate
P218 - Perl - More Objects
T245 - Tcl/Tk - [incr-Tcl]
H108 - Objects in PHP
U107 - Object Orientation - the Lua way

Back to
Icelandic Badge
Previous and next
or
Horse's mouth home
Forward to
Sorting objects in PHP
Some other Articles
FSB - an update.
Claverton Pump
Autumn
Sorting objects in PHP
Calling base class constructors
Icelandic Badge
Marc Schneider is still having email trouble
Holt on holt
Hotel Guest Surveys
Javascript/HTML example, dynamic server monitor
1889 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho