spacer
Yehuda Shiran February 13, 2001
Inheriting and Overriding Methods
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management
Inheritance is a very important requirement for an object-oriented language. JavaScript does not support any OO primitives in its language, but you can implement most of the stuff you need. Let's look at the following example:

function superClass() {

  this.bye = superBye;
  this.hello = superHello;
  
  function superHello() {
    return "Hello from superClass";
  }
  
  function superBye() {
    return "Bye from superClass";
  }
}

function subClass() {
  this.inheritFrom = superClass;
  this.inheritFrom();
  this.bye = subBye;
  
  function subBye() {
	return "Bye from subClass";
  }
}

var newClass = new subClass();
alert(newClass.bye());
alert(newClass.hello());

We have a super class , superClass, and a sub class, subClass. superClass defines two methods: bye() and hello(). subClass inherits these two methods by:

this.inheritFrom = superClass;
this.inheritFrom();
and overrides the bye() method. When clicking here, you trigger the evaluation of bye() and hello() from the sub class. You can judge from the alert boxes that indeed the hello() method is as defined by the super class, while the bye method is as overridden by the sub class.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags