Inheritance and Protected Sub

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JB

    Inheritance and Protected Sub

    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.GetEle ment()
    AnotherDerived. SubBase() 'Not accessible because it's protected !
    End Sub
    End Class

    Thanks
    JB
  • Armin Zingler

    #2
    Re: Inheritance and Protected Sub

    "JB" <jb.brossard@gm ail.comschrieb
    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.GetEle ment()
    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.GetEle ment()" to make it compilable)


    Armin

    Comment

    • Family Tree Mike

      #3
      Re: Inheritance and Protected Sub

      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()


      "JB" <jb.brossard@gm ail.comwrote in message
      news:cd2ca613-12a3-4106-be39-cf2540c8b88c@d1 g2000hsg.google groups.com...
      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.GetEle ment()
      AnotherDerived. SubBase() 'Not accessible because it's protected !
      End Sub
      End Class
      >
      Thanks
      JB

      Comment

      Working...