- Home /
Why does this run it's "super" method?
Here's a simplified example to explain my problem in Javascript:
I have a super-class:
class mySuperClass()
{
static function run()
{
return "mySuperClass";
}
}
And a sub-class which inherits from my super-class:
class mySubClass extends mySuperClass
{
static function run()
{
return "mySubClass";
}
}
And finally, a 3rd class:
class doThings
{
function Start()
{
var aClass = new mySubClass();
}
function Update()
{
Debug.Log(aClass); //This displays the text 'mySubClass'
var myText = aClass.run();
}
}
1) How comes myText equals "mySuperClass"? Surely it should run the mySubClass.run() method instead of the mySuperClass.run() method?
2) Is there any way to make it so that the super-class can never be instantiated (I know interface and abstract isn't supported)?
It worked ok for me, but I had to adjust it slightly to get it to compile. I realize you have simplified it for the example, but it may have lost something in translation.
class mySuperClass { static function run() { return "mySuperClass run"; } }
class mySubClass extends mySuperClass { static function run() { return "mySubClass run"; } }
var aClass;
function Start() { aClass = new mySubClass(); } function Update() { Debug.Log(aClass); //This displays the text 'mySubClass' var myText = aClass.run(); Debug.Log("myText = " + myText); }
Thanks for your time $$anonymous$$olix. I've managed to solve the problem ... I don't think the example I gave actually had the problem I was having in my actual code. $$anonymous$$aking my superclass functions Virtual solved the problem. Again, thanks for the effort!
Answer by JoelAtMoodkie · Apr 23, 2010 at 12:19 AM
I appear to have solved this myself.
I needed to make the functions in my superclass 'virtual' functions ... for some reason I thought virtual wasn't supported in Javascript, and thus I am a fool ;)
The Unity implementation of JavaScript isn't actually JavaScript. It's more... just the syntax of JavaScript adopted to .NET/$$anonymous$$ono. It's a pretty poor implementation, too, if you ask me. C# is a whole lot easier to understand and code with in the long run.
If we could vote up comments, I would vote up SpikeX's comment. $$anonymous$$ono JS is just enough like your browser's JS to be annoying (and too different to be useful as a "javascript implementation " I$$anonymous$$HO). If you are doing anything serious you should seriously consider using C#. It's more structured, but like Spike said, that buys you a lot in the long run.
You can vote up comments! Just hover your mouse to the left side of the comment and you have an up arrow you can press to vote for the comment.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inventroy Problem- Using different parameters in inherited classes 1 Answer
Interaction script 2 Answers
Null Reference Exception: Object Reference, with base class? 1 Answer
Confused about the pre-defined word "super" 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer