spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column110


JScript .NET, Part IV: Inheritance

Finance Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Overriding Methods and Properties

When you extend a base class, nothing prevents you from defining methods and properties for the new class with the same names as those of the base class. Who wins in such a name conflict? Does the base class have an overriding power over the new class? Or maybe the new class should take over the base class, as their names imply?

Luckily, JScript .NET is rich enough to let you choose the desired behavior. By default, a method or property marked with override will override the method or property of the base class that has the same name and is not marked with final (see Page 6). You cannot override a final method or property. You cannot mark classes, interfaces, and members of interfaces with override. Actually, the override marking is not a must. If the base method or property is not marked with final, the derived class will override the base class by default. If they are marked with final, the override marking won't help. Nonetheless, it is recommended to use the override label to make your code more readable and easier to maintain.

The hide marking addresses a similar but different need than the override label. The override label is very dominant. The derived method or property will override the base method or property, no matter how you define the variable type: either of the derived class or the base class. You don't have any way to restore the base class method or property. The hide label, however, depends on the context. If a variable is of the derived class type, the derived class methods and properties will override the base class's. If a variable is of the base class type, the base class methods and properties will be in effect and will be not be overridden by the derived class.

Let's look at an example that demonstrates the above behavior. The class FirstBase has two methods: pitcher and hitter, each printing a unique message:

class FirstBase {
  function pitcher() { print("I am a pitcher on first base"); }
  function hitter() { print("I am a hitter on first base"); }
}

Let's define a derived class now. It redefines both the pitcher() and the hitter() methods. The pitcher() method will be marked as hide, and the hitter() function will be marked as override:

class SecondBase extends FirstBase {
  hide     function pitcher()
    { print("I am a pitcher on second base"); }
  override function hitter()
    { print("I am a hitter on second base"); }
}

Let's create an instance of the derived class, SecondBase:

player : SecondBase = new SecondBase;

and call his pitcher() and hitter() methods:

player.pitcher();
player.hitter();

The output should reflect the default of overriding the base methods:

I am a pitcher on second base
I am a hitter on second base

But if we set a variable of type FirstBase, say player2, pointing to player, we get different results. The override function of the derived class SecondBase indeed overrides the base class method. The hide function of the derived class, though, does not override the base class, when the variable is of the base class type. Here is the code:

player2 : FirstBase = player;
player2.pitcher();
player2.hitter();

and here is the output:

I am a pitcher on first base
I am a hitter on second base


Next: How to avoid overriding base classes


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: May 20, 2002
Revised: May 20, 2002

URL: http://www.webreference.com/js/column110/5.html