Abstract classes and multiple inheritance

From searching the documentation, I don’t believe abstract classes can be created in Xojo?

What I would like to do is create subclasses of a control, let’s say CustomWebTextField that extends WebTextField where CustomWebTextField has its own specific methods and properties.
However I have several subclasses from different controls (WebRadioGroup, WebTextArea etc) which have the same custom method with either identical or similar implementations.

To save duplicating the method implementation, in my mind, I was thinking that I could setup an abstract class with the method implemented, then have the Custom subclasses inherit them. But not sure if a subclass control can inherit from both its superclass and an abstract class, that’s assuming abstract classes were even possible in Xojo.

What’s the best Xojo approach to solving my problem? I was thinking of using interfaces but it wouldn’t have any method implementations that I could implement across multiple objects.

Hi Samiul,
Create an Interface that each of your classes implements. You can then check if an object passed implements that interface using IsA or make your variables and parameters of the interface type and call methods of the interface on those objects.

Thanks, that makes sense.
However both options would still require me to duplicate method implementations in each class, if they were the same for instance? Since an interface can’t have method implementations.

Look into Extends. You can write a single implementation of an algorithm and “attach” it to multiple classes with a bit of class-specific code.

1 Like

Theoretically, in most cases, implementation details would be specific to the class. For example, if you had MyCoolInterface with a function MyCoolValue, and you wanted that on both MyWebTextField and MyWebRadioGroup, you would need to implement that interface method on each to return whatever value should be returned for that specific class.

You could use Variant, Extends methods, and a big block of code with IsA and/or Variant.Type, but an Interface compartmentalizes your code to your subclasses.

There are probably other ways to implement this that I’m not thinking of right now, but I know which route I’d take.

I agree although for cases where they are the same, it’s handy to have it in one place to prevent duplication. I guess I was looking at it from a Java point of view where having the same method implementation body in a group of classes can be achieved by creating an abstract class and implementing a method within it, then simply having classes extend the abstract class. And methods that require implementation separately in each class can be added as an abstract method in the abstract class. But since there’s no concept of abstract classes in Xojo, I’ll have a look at implementing one of the approaches mentioned by you :slight_smile:

You can have abstract classes in Xojo - if I understand the term. You just can’t have an object inherit from more than one class.

Hi Hassan,

A combination of Eric’s and Anthony Cyphers’ suggestions might get you closer if I’m correctly understanding what you want.

First…

Understand there’s a simpler solution if you’re not dealing with xojo’s built-in control classes, which is making an abstract class and making its constructor private (or protected if you want some shared constructor behavior across all subclasses?). You then add public constructors to the “abstract” class’ subclasses.

This prevents the abstract class from being instantiated at runtime, while all of its children can be instantiated.

This pattern doesn’t work with Xojo’s controls, as they act more like an implicit instance when you drag them onto a WebPage or WebContainer at Design Time. You can’t override their normal constructors so you can’t really block the base types from being created.

This is where Eric’s and Anthony’s suggestions can work together since…

Extends methods can extend interfaces, meaning you can treat the interface as the type being extended (vs. a class)

AND

Interfaces can aggregate other interfaces, which is hierarchically similar to class inheritance.

These features combined allow you to add something akin to shared methods from an abstract class to subclasses of Xojo controls.

I think this combination can get closer to what you’re aiming for.

I made a quick and dirty example project which you can download here: Dropbox - abstract_control_example.xojo_binary_project.zip - Simplify your life