Link to home
Start Free TrialLog in
Avatar of kimsoy
kimsoy

asked on

inheritance in VB

Are there inheritance features in VB like C++ or Java so that I can create subclasses that can inherit methods and properties from superclass?
Avatar of Mirkwood
Mirkwood

Nope. But what do you want to do or know. Because a one word answer is not worth 100 points.

You have interfaces though in VB but no inheritence of code. Sorry.
The 'Implements' keyword will allow you to inherit the interface, but not the implementaion.
ASKER CERTIFIED SOLUTION
Avatar of ameba
ameba
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kimsoy,
 As told by many above, in VB there is no code inheritance. yes u can have interface inheritace thru the IMPLEMENTS keword
u can inherit the interfaces and write ur code there
regards,
ajoy
Avatar of kimsoy

ASKER

Are there any downsides that I have to keep in mind?
Avatar of kimsoy

ASKER

I forgot to thank ameba, you have saved a lot of time and efforts. Thanks again...
kimsoy, if your SuperClass *definition* change, you are in trouble: Too many changes in your code, re-delegating code again...
You can simplify things, if you throw Implements and all its code, and use *only* instance of the base class, but you will loose type checking.
Some experts think inheritance is more trouble than benefits.
Avatar of kimsoy

ASKER

Here is the other way to do ingeritance that I got from other VB web site.  I didn't try this example yet, but it makes sence (I think...).

      'Class : clsStudent
      ' Contains a reference to a Person Object

      'private member variables
      Private oPerson as clsPerson
      Private iGrade as Integer

      Public Sub Create ( )
      'load this object and create the person object

           Set oPerson = New clsPerson

      End Sub

      Public Property Let Name ( sNewName as String)
      'set the person objects Name Property

           oPerson.Name = sNewName

      End Property

      Public Property Get Name ( ) as String
      'retreive the person objects Name Property

           Name = oPerson.Name

      End Property


How is it different from the answer I was given?
Which way is better (less trouble)?
   
Yes, this is less trouble (without Implements).
I think, you should remove "Create" method and use:

Private Sub Class_Initialize()
    Set oPerson = New clsPerson
End Sub

Private Sub Class_Terminate()
    Set oPerson = Nothing
End Sub

Well, someone can say this is not Inheritance, but Delegating.
--

Implements can help if you have a Method that expects BaseClass as argument:

Public Function IsRetired(objItem as clsPerson) As Boolean
    If Left$(objItem.Name, 3) = "em." Then
        IsRetired = True
    End If
End Sub

Without Implements, this will produce type-mismatch error:
Set objStudent = New clsStudent
x = IsRetired(objStudent)

By adding Implements you can avoid this error.

But this works only one level deep: If you have clsEEStudent which implements clsStudent, you will not be able to use IsRetired method - clsEEStudent doesn't have Implements clsPerson line in the module :(