Overwriting a function in a subclass

You can not change the return value of an overwritten method, or more specifically “narrow” its type:

Function GiveValue() As Variant // in Class A // These will not compile: Function GiveValue() As Integer // in Class B, which is a subclass of A Function GiveValue() As String // in Class C, which is also a subclass of A
But if the return values of the parent class is Object, you can narrow the type of the return value in the subclass (for example from Object to SomeObject123 and SomeObjectXYZ):

Function GiveValue() As Object // in Class A // These will compile: Function GiveValue() As SomeObject123 // in Class B, which is a subclass of A Function GiveValue() As SomeObjectXYZ // in Class C, which is also a subclass of A
Is this in the docs? Actually I didn’t find anything about overwriting in the docs. Does Xojo use another word for it?
I googled for:
site:documentation.xojo.com overwrite
site:documentation.xojo.com overwriting
site:documentation.xojo.com overwritten

To override a method you must provide in a subclass a method with the same signature of the overridden one.
What are you trying to do, is a “cross-overloading” (if a such term exists).

In your second example, really your are overriding. SomeObject123 is still an Object since it’s a subclass of Object.

I think this is a problem specific to using Variant, not an issue with overloading a method. And just because the code compiles, doesn’t mean it does what you expect. I don’t think your example with Object is valid.

Thanks, but my question was, where is the subject “Overwriting” in the docs?

Look for “inheritance” or similar. “Overwriting” is certainly the wrong term. Even “overloading” isn’t quite right. You’re looking at the concept of “virtual functions”. You won’t find anything in the Language Reference, as it isn’t a language construct. Look in the User’s Guide.

Please see if this helps.

Similar details can also be found elsewhere on the Internet.

Oops, the term is “Overriding”, not “Overwriting”. Stupid me…

The User’s Guide is a minimalistic masterpiece… An example: for “Overloading” it doesn’t even mention that you can’t overload based on the different return types only.

My assumption now - after your answers (and a bit more thinking) - is, that since you can’t override and at the same time overload a function, the statement is valid - it is treated as just a completely different method.

You can overload methods - but the return type is not part of the signature so changing JUST the return type won’t do what you expect.

Just to try and help and to make sure that I understand it in Xojo world too

Overload

You can overload a method by using different parameters, as Norman states the return type should remain the same and is ignored, e.g.
Foo(val as integer) as string
Foo(val as double) as string

If you change just the return type for the overloaded methods then Xojo will complain that it doesn’t know which method to use, interestingly if you change both the signature and the return type so that all combinations are unique then Xojo happily compiles.

So you seem to be able to add an overload like:

Foo(val as boolean) as integer

and Xojo can work out which method to call based on the parameters and is happy with the different return type.

Override

You can override the behaviour of a method in a subclass, as Massimo states the whole signature should be the same but the implementation can be different (otherwise why override it?)

Superclass:
Class1 -> Foo(val as string) as string
return "My name is Class1, you sent " + val

Class1 -> Foo(val as double) as string
return "My name is Class1, you sent " + str(val)

Subclass of Class1:
Class2 -> Foo(val as string) as string
return "My name is Class2, you sent " + val

Class1 -> Foo(val as double) as string
return "My name is Class2, you sent " + str(val)

But you see in all cases above you are committed to returning a string because only the method name and parameters are considered for overloading and the signatures must be the same for overriding.

I hope that I got this right :slight_smile: and I just had a thought - what happens if you only override one of the superclass overloaded methods in a subclass? I guess it should use the superclass overloaded method for the other but does it in Xojo? I will try it out one day unless someone posts the answer here.

[quote=24039:@Carl Clarke]Just to try and help and to make sure that I understand it in Xojo world too

Overload

You can overload a method by using different parameters, as Norman states the return type should remain the same and is ignored, e.g.
Foo(val as integer) as string
Foo(val as double) as string
[/quote]
Say you did

dim i as int64
dim s as string = Foo(i)

While you can write this calling it will likely give you some grief since these are both intrinsic numeric types and either could be called.
And you have to also watch when you have calls the use default parameters say

Foo(val as integer, b as boolean = false) as string
Foo(val as double) as string

dim i as int64
dim s as string = Foo(i)

The call to Foo is still ambiguous

Yes as it can unambiguously determine which to call

correct

[quote=24039:@Carl Clarke]
Override

You can override the behaviour of a method in a subclass, as Massimo states the whole signature should be the same but the implementation can be different (otherwise why override it?)

Superclass:
Class1 -> Foo(val as string) as string
return "My name is Class1, you sent " + val

Class1 -> Foo(val as double) as string
return "My name is Class1, you sent " + str(val)

Subclass of Class1:
Class2 -> Foo(val as string) as string
return "My name is Class2, you sent " + val

Class1 -> Foo(val as double) as string
return "My name is Class2, you sent " + str(val)

But you see in all cases above you are committed to returning a string because only the method name and parameters are considered for overloading and the signatures must be the same for overriding.

I hope that I got this right :slight_smile: and I just had a thought - what happens if you only override one of the superclass overloaded methods in a subclass? I guess it should use the superclass overloaded method for the other but does it in Xojo? I will try it out one day unless someone posts the answer here.[/quote]

When an instance of the subclass has one of the methods invoked it will use the correct implementation - which may be the subclass provided one or the super class one.

Static methods can be overloaded, that means a class can have more than one static method of same name. But static methods cannot be overridden, even if you declare a same static method in derived class it has nothing to do with the same method of base class…Source

Dov