Subclass or extension methods, oh my

Dear All,

Let’s say I want to add some methods to class. I will call the existing class 1stGrade. For my implementation, I need to add a multiplication and a division method to 1stGrade to make it useful for @ndGrade.

My questions:

  1. In general, how does one decide between an extension method versus adding methods to a subclass. Assume no properties need to be added to parent class.

    • I know this has been discussed before, but those discussions appear to be at least 8 years old and new approaches may now be available or desired.
  2. Do extension methods pass to subclasses?



1/ you make extensions methods for classes you don’t have access, like xojo framework classes
or classes someone else wrote, and are too complicated for you to dig in and modify
if you have access to a class, then set the method in that class

2/ yes.


Putting the method in the class is much cleaner and easier to maintain. The methods are listed with the class so you can see them. Extension methods are not. I’m currently working in another environment where you have to put code in 3 separate places to define a class. It’s driving me nuts. I miss seeing everything in one place all at once.

1 Like

An exception would be interfaces. You can extend an interface to add common functionality.

I am not a friend of those languages that claim to be object-oriented, but maintenance shows the opposite.

I’ve already come across one of these.

I’m curious. What language did you have to observe in three different places for a single method?

That’s not what he said.

Let’s take Rust for example (there are more languages similar to its behavior). In Rust there are no Class as in OOP, but we have similar things (something holding data and methods as one logical entity) but they are coded apart and glued logically, a struct (data) and trait implementations for that struct, of some trait definitions (3 different places to define a “Class”)
Remove one, and “the class” is broken.


I’m apparently to ignorant to understand your response or to know how it is helpful. I apologize.



He was asking Tim, not you Thomas.

1 Like

Not a language per se. Microsoft Entity Framework for MSSQL database. It’s a pain in the neck and gives me the night sweats.

@Thomas_Wilcox I prefer the way Xojo’s IDE presents class methods versus extension methods. Extends is a powerful concept and is perfect for when you can’t modify the class directly.

1 Like

seems you will share a few methods across all xGrade, its like you have a base class or a AllGrade class.
extension is need as example if you will give xojo string class a new feature.
a interface is used if all classes have the same method as example load/save.
base class is used to share the same data for classes that have the same need, as example position,size,rectangle,id.

to prevent copy paste
you could have a method in 1stGrade.division which call a shared method AllGrade.division

class 1stGrade
method division(a,b)
return AllGrade.division(a,b)

class 2ndGrade
method division(a,b)
return AllGrade.division(a,b)

disadvantage, it can not run parallel on multiple cpu’s

If you need to call different versions of the multiply/divide/etc. methods depending on which subclass they’re called with, then using extension methods won’t work.

Extension methods follow the normal rules of method overloading, while subclass methods follow the special rules of method overriding. The main difference is how they treat an instance of a subclass when its referred to by its superclass. Extension methods see the reference type; class methods see the instance type.


Module MyModule
	Sub Multiply(Extends f As 1stGrade)
	Sub Multiply(Extends f As 2ndGrade)
End Module

Dim f As 1stGrade ' reference to a 1stGrade
f = New 2ndGrade ' instance of a 2ndGrade
f.Multiply() ' Multiply(Extends 1stGrade) -- WRONG 


Class 1stGrade
	Sub Multiply()
End Class

Class 2ndGrade
Inherits 1stGrade
	Sub Multiply()
End Class

Dim f As 1stGrade ' reference to a 1stGrade
f = New 2ndGrade ' instance of a 2ndGrade
f.Multiply() ' 2ndGrade.Multiply() -- RIGHT

Thank you all for your input. I now have a clearer idea of extension methods. The OOP idea is very simple in principle, until one delves deeper into the turf. Then you run into worms and little mites and most of all fungi; all useful but need special care and knowledge before use.

Merry Christmas and Happy Holidays, etc. to all.