Results 1 to 6 of 6

Thread: [RESOLVED] Class hierarchy question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    514

    Resolved [RESOLVED] Class hierarchy question

    Hi all.

    I have a question with regards to polymorphism. The way I understand it, that's what I'm using, but I'm not sure I'm doing it right, at least in the particular case I'll discribe here.

    I have a class A that has two subclasses A1 and A2. Class A will never get instanciated on it's own, only A1 and A2 will (so A is a 'mustinherit' class). The two subclasses share a method (lets call it DoSomeStuff) that behaves differently in each class A1 and A2. Now if I want to call that method when I'm holding a reference to an object declared as A (although the actual object is either A1 or A2), the best way I have found is to declare the method as Overridable withing class A, but without any code. (That's where I feel I may not be doing things right).

    Here's what I have just explained in code:

    Class A:
    Code:
    Public MustInherit Class A
    
         Public Overridable Sub DoSomeStuff()
    
              'Do nothing here, since method is overrode is class A1 and A2...
    
         End Sub
    
    End Class
    Class A1:
    Code:
    Public Class A1
        Inherits A
    
         Public Overrides Sub DoSomeStuff()
    
              Console.WriteLine("Hey!  I'm Class A1!  Ain't that cool?")
    
         End Sub
    
    End Class
    Class A2:
    Code:
    Public Class A2
        Inherits A
    
         Public Overrides Sub DoSomeStuff()
    
              Console.WriteLine("Hey!  I'm Class A2!  You bet it's cool!")
    
         End Sub
    
    End Class
    So my question is, is this considered a 'by the book' way of doing things? Or would it be better to implement an interface on both A1 and A2?

    Thanks for your feedback on this.
    Don't ask why, just reboot!

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    514

    Re: Class hierarchy question

    Oh, well. Once again Google helped me. I just typed "vb.net mustinherit class sub with no code" and this came up:

    https://msdn.microsoft.com/en-us/lib...=vs.71%29.aspx

    So my Class A would be an abstract class. I guess I was doing things right afterall!
    Don't ask why, just reboot!

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Class hierarchy question

    Yeah, that's right.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    514

    Re: [RESOLVED] Class hierarchy question

    Thanks for confirming, Shaggy.
    Don't ask why, just reboot!

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Class hierarchy question

    If DoSomeStuff has no implementation in A and the implementation must be supplied by an inheriting class, you can mark the method as MustOverride rather than have an empty implementation: https://msdn.microsoft.com/en-us/library/hyb29zk8.aspx

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    514

    Re: [RESOLVED] Class hierarchy question

    Yes, thanks for pointing this out.
    Don't ask why, just reboot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width