Results 1 to 3 of 3

Thread: Inheritance Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Inheritance Question

    When you inherit an object into a class, If you don't delcare a constructor, will it use the inherited objects constructor by default?

    Code:
    Public Class clsArrayList
        Inherits ArrayList
    
        Public Function ReturnTypeList(ByVal inType As Type) As ArrayList
    
            Dim retList As New ArrayList
    
            'Return a new list by object type
            For Each o As Object In Me
                If o.GetType Is inType Then retList.Add(o)
            Next
    
            Return retList
    
        End Function
    
    End Class
    Or do I have to add these to use the default conscrutors?

    Code:
        Public Sub New()
            MyBase.New()
        End Sub
    
        Public Sub New(ByVal capacity As Integer)
            MyBase.New(capacity)
        End Sub
    
        Public Sub New(ByVal c As System.Collections.ICollection)
            MyBase.New(c)
        End Sub

    Next question, I am eventually going to be Serializing this arraylist to a XML file using the XmlSerializer and from what I understand that the ArrayList object you serialize needs to Implement an IColletion interface, An arraylist does this by default, Does my class above still implement the ICollection since it's based off a ArrayList object or do I need to specify that as well?

    Thanks

    Hinder

  2. #2
    Addicted Member
    Join Date
    Mar 2005
    Posts
    142

    Re: Inheritance Question

    Your inherited object gains the constructor and interface(s) of its parent class.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Inheritance Question

    Actually Constructors are the only thing not passed to a derived class. So it would inherit the interface but not the constructors. You can call them as you show but you will need to write that code it is not implied via inheritance.

    Nevermind I guess it does inherit and fire the parent classes default constructor but it doesn't inherit any of the non default constructors.

    So calling New on your child class will use the New on the parent class the same as:
    VB Code:
    1. Public Sub New()
    2. Mybase.New()
    3. End Sub

    But the child class will not have the other constructors through inheritance.

    VB Code:
    1. Public Class Parent
    2.  
    3.     Public Sub New()
    4.         MsgBox("Showing")
    5.     End Sub
    6.  
    7.     Public Sub New(ByVal msg As String)
    8.         MsgBox(msg)
    9.     End Sub
    10.  
    11. End Class
    12.  
    13. Public Class Child
    14.     Inherits Parent
    15.  
    16. End Class
    17.  
    18. 'syntax of use
    19.         Dim c As New Child 'only default constructor is available
    Last edited by Edneeis; May 11th, 2005 at 10:26 PM.

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