Inheritance and Protected Sub

  • Thread starter Thread starter JB
  • Start date Start date
J

JB

Hi All,

I've just experienced a "strange" behaviour with inheritance and the
Protected keyword.
Can someone please shed a light on this? Is this the normal behaviour?

Class Base
Protected Sub SubBase()
'Do Stuff
End Sub
End Class

Class Derived Inherits Base
Protected Sub Test()
'Call to parent Protected Sub OK
MyBase.SubBase()

'Now trying to call SubBase on a different instance of Derived
doesn't work !
Dim AnotherDerived as Derived = SomeList.GetElement()
AnotherDerived.SubBase() 'Not accessible because it's protected !
End Sub
End Class

Thanks
JB
 
JB said:
Hi All,

I've just experienced a "strange" behaviour with inheritance and the
Protected keyword.
Can someone please shed a light on this? Is this the normal
behaviour?

Class Base
Protected Sub SubBase()
'Do Stuff
End Sub
End Class

Class Derived Inherits Base
Protected Sub Test()
'Call to parent Protected Sub OK
MyBase.SubBase()

'Now trying to call SubBase on a different instance of Derived
doesn't work !
Dim AnotherDerived as Derived = SomeList.GetElement()
AnotherDerived.SubBase() 'Not accessible because it's protected !
End Sub
End Class


Can't repro the problem. Can be compiled here (VB 2008). (after removing "=
SomeList.GetElement()" to make it compilable)


Armin
 
Right, protected means an inheriting class can call it inside the inheriting
class, but you cannot call it on an instance. Also, this will not work:

Dim b as New Base
b.SubBase()
 
Back
Top