473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inheritace and super.class calling

Hi,

I was wondering whether someone could shed some light on the following.
Using inheritance in Java one can override a function f() (or is it
overload?) in the child and then do:
public f() {
super.f();
...
}
in the child to first execute the parent stuff to be followed by the
additional child stuff.

Is there a way to accomplish this in JS as well and if so how?

I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.

TIA,
Fermin DCG

Jul 20 '05 #1
5 6502
Da Costa Gomez <dc*@xs4all.nl> writes:
I was wondering whether someone could shed some light on the following.
Using inheritance in Java one can override a function f() (or is it
overload?) in the child and then do:

public f() {
super.f();
...
}
in the child to first execute the parent stuff to be followed by the
additional child stuff. Is there a way to accomplish this in JS as well and if so how?
Not directly.

Javascript doesn't have classes. As such, there is no notion of a
super class. All you have are singular objects, and prototype
inheritance.

Assume we have an object with a method "bar" in its prototype.

In Javascript there is no simple way to access the prototype of an
object (like the "super" keyword in, e.g., Java). You can use
"Foo.prototype.bar" (if the constructor function used to create the
object was "Foo"), but it is not absolutely safe. Someone can have
changed the prototype property of Foo since the object was created.

o.bar = function() {alert("The answer is: "); Foo.prototype.bar();}

You can rename the old function before shadowing it:

o.oldBar = o.bar;
o.bar = function() {alert("The answer is: "); this.oldBar();}

You can even make it a local variable, only available to the new bar
function:

o.bar = (function(oldBar) {
return function() {alert("The answer is: "); oldBar.call(this);}
})(o.bar);
I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.


You could try looking at Smalltalk or Self. They are closer to
Javascript in Object methodology (as prototype based languages rather
than class based).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
> I was wondering whether someone could shed some light on the following.
Using inheritance in Java one can override a function f() (or is it
overload?) in the child and then do:
public f() {
super.f();
...
}
in the child to first execute the parent stuff to be followed by the
additional child stuff.

Is there a way to accomplish this in JS as well and if so how?

I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.


Yes, check out http://www.crockford.com/javascript/inheritance.html

Since JavaScript is class-free, you have a lot more options than Java-like
classical inheritance. Usually, when you are looking to extend but super, what
you really want to do is augment.

Jul 20 '05 #3
Douglas Crockford wrote:
I was wondering whether someone could shed some light on the following.
Using inheritance in Java one can override a function f() (or is it
overload?) in the child and then do:
public f() {
super.f();
...
}
in the child to first execute the parent stuff to be followed by the
additional child stuff.

Is there a way to accomplish this in JS as well and if so how?

I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.

Yes, check out http://www.crockford.com/javascript/inheritance.html

Since JavaScript is class-free, you have a lot more options than Java-like
classical inheritance. Usually, when you are looking to extend but super, what
you really want to do is augment.


Read the piece and there is one thng that still eludes me though.
I'm using the following to extend classes:

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
}

This would take care of all the methods available in the parent to be
'send' to the child.

I am assuming that something like:

ChildClass.prototype.overriddenFunction = function {
// Execute this.uber("overriddenFunction");
// ... method particular stuff
return this;
}

Should basically seal it for me. Or am I completely wrong in the
assumption that *ChildClass.prototype.overriddenFunction* gets the job
done of overriding the parent version?

Concerning the uber function I can appreciate the apply bit but I just
do not see how one would seperate the uber function from the
encapsulation method function so that it can be 'executed' similar to
super().

Or am I way of and spinning around with a blind-fold on?

TIA
Fermin DCG

Creating properties (+ getters & setters) is done by calling 1 method only.

Jul 20 '05 #4
> Or am I way of and spinning around with a blind-fold on?

Maybe so. While it is possible to approximate the Java style of classes in
JavaScript, I don't think it is, ultimately, wise. Greater efficiencies come in
JavaScript with the augmentation patterns. These, I feel, more than compensate
for the lack of strong typing. If you really want to be writing in the classical
style, you would do better to stick with Java. JavaScript feels you from the
need to construct complex class hierarchies. Each object is just exactly what it
needs to be, with no class overhead.

http://www.crockford.com/javascript/little.html

Jul 20 '05 #5
Da Costa Gomez wrote:
Hi,

I was wondering whether someone could shed some light on the following.
Using inheritance in Java one can override a function f() (or is it
overload?) in the child and then do:
public f() {
super.f();
...
}
in the child to first execute the parent stuff to be followed by the
additional child stuff.

Is there a way to accomplish this in JS as well and if so how?

I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.

TIA,
Fermin DCG

After studying both suggestions I came up with the following solution (&
1 nagging question).
Solution:

function ClassA () {
}

ClassA.prototype.baseFunction = function() {
return ("ClassA::baseFunction() called");
};

ClassA.prototype.deriveFunction = function() {
return ("ClassA::deriveFunction() called");
};
function ClassB () {
A = new ClassA();
this.extend(A); // Takes care of 'copying' the whole ClassA structure

// Override function code
_deriveFunction = A.deriveFunction;
this.deriveFunction = function() {
return("ClassB::deriveFunction() called with " + _deriveFunction());
}
}

Doing this allows me to keep ClassA as it is and ClassB carries the
deriveFunction functionality of A *plus* the added functionality of itself.

The question that is left:
Using
ClassB.prototype.deriveFunction = function() {
return ("ClassB::deriveFunction() called"+_deriveFunction());
};

instead of the this.deriveFunction IN ClassB will MOT get the desired
result. Instead it will always stay equal to the ClassA version.
Does it have something to do with the public nature of the ClassA
definition?

Thx for your input.
Cheers,
Fermin DCG
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kerim Borchaev | last post by:
Hello! Always when I use "super" I create a code duplication because class used as first arg to "super" is always the class where the method containing "super" was defined in: ''' class C:...
2
by: Clarence Gardner | last post by:
The super object is considered a solution to the "diamond problem". However, it generally requires that the ultimate base class know that it is last in the method resolution order, and hence it...
10
by: Chris Green | last post by:
Good day, I've done a bit of searching in the language reference and a couple pages referring the behavior of super() but I can't find any discussion of why super needs the name of the class as...
5
by: Tobiah | last post by:
What is the purpose of the second argument to super()? What is meant by the returning of an 'unbound' object when the argument is omitted. Also, when would I pass an object as the second...
6
by: Steven Bethard | last post by:
When would you call super with only one argument? The only examples I can find of doing this are in the test suite for super. Playing around with it: py> class A(object): .... x = 'a'...
2
by: Michael P. Soulier | last post by:
Ok, this works in Python on Windows, but here on Linux, with Python 2.4.1, I'm getting an error. The docs say: A typical use for calling a cooperative superclass method is: class C(B):...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
4
by: Noah | last post by:
Am I the only one that finds the super function to be confusing? I have a base class that inherits from object. In other words new style class: class foo (object): def __init__ (self, arg_A,...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.