- Home /
Won't excecute the overwrited function
Hi guys, I've got a little problem with custom classes and inheritance.
I've got an empty base class, for example
class A{
public void Acoolfunction(){
//do nothing
}
}
but now I've made some other clases
that inheritate from this class. Lets give it an other example:
class B : A{
public void Acoolfunction(){
Debug.Log("Hello World");
}
}
class C : A{
public void Acoolfunction(){
Debug.Log("Cya World");
}
}
Now we come to the tricky part:
I need a list that can contain both classes, B and C. So I created an array of A and added B's and C's to it. My problem is, that when I try to call the Acoolfunction of my listed classes, he will allways call the function in A, not the functions from B and C that overwrited it.
So I know the problem, but if I remember right it should have worked that way :/ How can I get it to actually call the right method and not the parent typ method?
Thanks for any help.
btw. in program$$anonymous$$g we don't use the word overwritten (from overwrite) because the function is overridden (from override) which means bypass / replace in this context and not literally overwritten ;)
Ahh, good to know, I am from germany and here we say "überschrieben" what means overwritten and "ersetzt" what would be replaced, it kinda depends who is talking to you, it seems everyone uses the word he likes here^^ But thanks, now I know at least how to say it correctly in english =)
Answer by whydoidoit · Jan 18, 2013 at 12:23 AM
You need to specify that Acoolfunction is virtual and then override it in the subclasses:
class A {
public virtual void Acoolfunction() {
}
}
class B : A {
public override void Acoolfunction() {
Debug.Log("Hello world");
}
}
Answer by liszto · Jan 18, 2013 at 12:23 AM
If your first class A do nothing why don't do this :
class A{
public virtual void Acoolfunction(){
//do nothing
}
}
then this for the others :
class B : A{
public override void Acoolfunction(){
Debug.Log("Hello World");
}
}
class C : A{
public override void Acoolfunction(){
Debug.Log("Cya World");
}
}
I think this can work.
Answer by Nerethar · Jan 18, 2013 at 12:44 AM
Wow, 2 correct answers at the same time, it was a hard choice, who gets the mark for the right answer^^ Thank you both very much, now it is working. Sadly, for object based languages I'm just used to Java, where you can simply name the function the same way to override it^^
Good night guys and thanks again =)
You can (legally) overwrite the function by using the new keyword - but then as you saw, it doesn't make it virtual, it just lets you redefine it which can be useful (but not for what you are doing!)
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Problem using variable of type CustomStat to indicate which player statistic to change 1 Answer
FPS weapons class structure 1 Answer
HideInInspector with inherited variables 1 Answer
Cram variables inside class, use inheritance, or something else? 1 Answer