473,569 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6514
Da Costa Gomez <dc*@xs4all.n l> 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.b ar();}

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(oldBa r) {
return function() {alert("The answer is: "); oldBar.call(thi s);}
})(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.prototyp e.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.prot otype.overridde nFunction = function {
// Execute this.uber("over riddenFunction" );
// ... method particular stuff
return this;
}

Should basically seal it for me. Or am I completely wrong in the
assumption that *ChildClass.pro totype.overridd enFunction* 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.prototyp e.baseFunction = function() {
return ("ClassA::baseF unction() called");
};

ClassA.prototyp e.deriveFunctio n = function() {
return ("ClassA::deriv eFunction() called");
};
function ClassB () {
A = new ClassA();
this.extend(A); // Takes care of 'copying' the whole ClassA structure

// Override function code
_deriveFunction = A.deriveFunctio n;
this.deriveFunc tion = 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.prototyp e.deriveFunctio n = function() {
return ("ClassB::deriv eFunction() called"+_derive Function());
};

instead of the this.deriveFunc tion 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
2313
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: def method(self): super(C, self).method() '''
2
1666
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 should not itself use super (as well as supplying the ultimate implementation of an overridden method.) However, a problem comes up if that ultimate...
10
2118
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 an argument. Feel free to point me into the bowels of google if this has been discussed to death already. super(self).method() seems like super...
5
2352
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 argument, and when would I pass a type? Thanks,
6
2002
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' .... py> class B(A): .... x = 'b' ....
2
2536
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): def meth(self, arg): super(C, self).meth(arg)
4
1714
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
3262
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, arg_B): self.a = arg_A self.b = arg_B # Do I need to call __init__ on "object" base class?
10
13283
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>"; print(self.text); def __del__(self): self.text = "</BODY></HTML>"; print(self.text);
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.