Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
JVP's mid-tier inheritance challenge
Message
From
31/01/2000 11:25:15
 
General information
Forum:
Visual FoxPro
Category:
Classes - VCX
Miscellaneous
Thread ID:
00324138
Message ID:
00325046
Views:
31
Thanks John,

I pretty much had the correct concepts. You clarifed things where I was a bit fuzzy.

>Hi Craig..
>
>OK... you know what the interface is. It is the behaviors and attributes of a class. It is a contract. And, like any contract, it needs to respected. Otherwise, things breakdown and don't work.
>
>So, lets say we have an abstract animal class like this:
>
>
>Option Explicit
>
>Public Sub Action()
>
>End Sub
>
>
>Our abstract animal is capable of one thing, an action.
>
>Now, lets create a rabbit that inherits the abstract animal interface:
>
>
>Option Explicit
>
>Implements AbstractAnimal
>
>Private Sub AbstractAnimal_Action()
>   Msg "Hop"
>End Sub
>
>
>Now, lets create a dog:
>
>
>Option Explicit
>
>Implements AbstractAnimal
>
>Private Sub AbstractAnimal_Action()
>   Msgbox "Wag Tail"
>End Sub
>
>
>Now, lets create some code that instances these classes:
>
>
>Dim animal As AbstractAnimal
>Dim rabbit As New rabbit
>Dim dog As New dog
>' Keep in mind, it is the animal class that has the PUBLIC interface
>' So, we need to use animal. However, by itself means nothing. So, we
>' in effect overlay one of our implementing classes to give it life
>Set animal = rabbit
>animal.Action ' hop
>Set animal = dog
>animal.Action ' wag tail
>
>
>The key to all of this is by implementing the interface of a class, I am FORCED to implement all of the public methods and properties. If you don't you will get a compile error.
>
>Now, lets say you have some common code that all animals need to run. Myth will tell you that you need to copy and paste the code to each class that implements the abstract animial interface. The fact is, you don't. However, it does not come for free. But, the price you pay, if you design things correctly, is not that bad.
>
>Also in the modified example, we have outfitted the abstract class with a name property to indicate what type of animal is involved when the class is instanced via an implementing class:
>
>
>Option Explicit
>
>Public name As String
>
>Public Sub Action()
>
>End Sub
>
>Public Sub Feed()
>   MsgBox "feeding the  " & name
>End Sub
>
>
>here is the modified rabbit class:
>
>
>Option Explicit
>
>Implements AbstractAnimal
>Dim oAnimal As New AbstractAnimal
>
>Private Sub AbstractAnimal_Action()
>   MsgBox "Hop"
>End Sub
>
>Private Sub AbstractAnimal_Feed()
>   oAnimal.Feed
>   MsgBox "Don't need to do anything with Rabbit after feeding"
>End Sub
>
>Private Property Let AbstractAnimal_name(ByVal RHS As String)
>
>End Property
>
>Private Property Get AbstractAnimal_name() As String
>
>End Property
>
>Private Sub Class_Initialize()
>   oAnimal.name = "Rabbit"
>End Sub
>
>
>Here is the modified dog:
>
>
>Option Explicit
>Implements AbstractAnimal
>Dim oAnimal As New AbstractAnimal
>
>Private Sub AbstractAnimal_Action()
>   MsgBox "Wag Tail"
>End Sub
>
>Private Sub AbstractAnimal_Feed()
>   oAnimal.Feed
>   MsgBox "Need to take dog for walk after feeding"
>End Sub
>
>Private Property Let AbstractAnimal_name(ByVal RHS As String)
>
>End Property
>
>Private Property Get AbstractAnimal_name() As String
>
>End Property
>
>Private Sub Class_Initialize()
>   oAnimal.name = "Dog"
>End Sub
>
>
>The key is that internally, each class creates an instance of the base animal class to invoke the common behavior. Then, each instance has it's own behavior.
>I don't know if I would do things exactly like this, but it does go to show how implementation inheritance can be simulated in VB. And, it shows the benefits of interface inheritance.
>
>In this scenario, would automatic inheritance be nice? Sure, it would obviate the need for me to create an internal instance of the base class.
>
>Any way, that is the difference..
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Previous
Reply
Map
View

Click here to load this message in the networking platform