Overloading - Inheritance vs. Interfaces

I think the general answer to this question will be “don’t use overloading” however…

I have 2 classes:

subClass1 (subclass of 1)
subClass1 also has an interface attached - Int1

I have 2 methods, overloaded:

doSomething(param1 as class1)
doSomething(param1 as int1)

Now if I call doSomething(subClass1), which of the methods is called, and why?

It looks like inheritance take precedence over interface. How can I force it to use the other one?

I don’t have much experience using interfaces. Instead, if I have it modeling and programming in objects.

The interface seems to me very closed and elementary.

I’ve gone the way of classes and using inheritance.

In this case I use both as inheritance and interfaces as the latter allows me to refer to quite unrelated classes but in a common way. The method call is usually followed by a series of select case, case isa xyz, case isa vwx etc selections.

I don’t think you can.

Without knowing the details, my first thought upon seeing your comment about using select case statements was, “he needs to rethink this design.” But that’s not what you asked…

I’d have to test, but if you assign your subclass to an int1 variable first, it might force the version you want.

var myInt1 as Int1 = mySubclass
doSomething( myInt1 )

I did try that Kem, but no joy.

Obviously there are other work-arounds but this was just interesting.

1 Like


You got me curious, so I just tried this. Because you didn’t write any “actual code,” I’m assuming it looks something like this:

Dim c As New class1
Dim cc As New CustomClass1
Dim cc2 As New CustomClass1

class1 is the parent
customClass1 is a subclass of class1 with a single method:
Public Sub doSomething(param1 as class1)

int1 is an interface, also with a single method:
Public Sub doSomething(param as int1)

CustomClass1 has the int1 Interface applied to it so that both methods appear.

When I run my project, I get a compile error saying there is more than one method with that name but that this does not match the available signatures.

Now that said, you can cast the parameter like this to get it to use a particular method:


Here’s my project


(Moving those methods up to Class1 does not change the behavior and the casting still works)

1 Like

Thanks Greg,

I had a slightly different implementation (in attached), where cc wasn’t an Int1, but I think the result is the same. Of course the problem I had was deep in other code and not so simple to extract, but it was continually accessing the wrong overloaded method - or so I thought.

This part of the code works fine - and illustrates that the initial casting is key.

Var c1 As class1 = New subclass1 
Var c2 As subClass1 = New subClass1
Var c3 As int1 = New subClass1
Var cc1 As New cc

cc1.doSomething(c1)  //param as class1
cc1.doSomething(c2)  //param as subClass1
cc1.doSomething(c3)  //param as int1

I’m still trying to figure out why my earlier code was giving the wrong answer, possibly the original casting wasn’t as expected though I’m not sure how that would have happened.

My version…

Are you creating the doSomething method to start the polymorphism?
Using an interface to solve it.