Abstract Classes?

I can’t believe I’ve never noticed this, but it appears that Xojo does not support any way of declaring an abstract class - is this true? Maybe I’m just blind, but I can’t find any mention of it in the docs, forums, etc.

I don’t really want to use them all that often, but occasionally I do prefer to be able to create certain classes in my object hierarchies that need to be there for shared functionality / properties, but I don’t want the class instantiated directly.

i.e., if I have a class foo (with methods and properties implemented in it), and 2 children of foo named bar and baz, but I never want to even be allowed to instantiate foo itself… only bar and baz. Is this possible in xojo?

Note this is very different from a class interface, which merely defines methods that must be implemented, and does not have any properties or implementations itself.


It’s true, there is no concept of an abstract class.

If you don’t want the class to be instantiated, give it a private constructor.

Thanks, Paul. That’ll do for my purposes.

Out of curiosity, why is this omitted from the language? It’s such a common paradigm in traditional OOD languages. Are there any plans to add something like this?

You could also raise a new runtime exception in the constructor of the foo class so that the constructor must be overridden in bar and baz.

Personally, I’d prefer a compilation error, which is what you get with a private constructor, rather than a run-time error.

I know of no plans to add this, nor do I know why it was not included in the language. I’d guess it was considered an unnecessary advanced feature at the time.

I agree, compiler errors are always better to deal with than runtime errors. I don’t see why the compiler could not be taught to be aware of abstract classes and barf when one tries to be instantiated.

At any rate, thanks for the info. Private constructor works for me.

The language, as it sits, makes its pretty easy to make one by just having protected constructors / private constructors.
There’s no advantage to adding syntax just for this since there are way to achieve the same effect - a class that cannot be instantiated but can serve as the parent for others.

So sayeth the compiler & language god :stuck_out_tongue:

Basically you can already do this and adding “abstract” as a keyword buys you nothing new.

Please note that there is a difference between having the standard constructor private or protected:

  • protected: the subclass’s constructor needs to be overridden
  • private: the compiler adds an implicit and public standard constructor for the subclass