Results 1 to 4 of 4

Thread: Example of Inheritance from Parent Class to a Child Class

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    14

    Question Example of Inheritance from Parent Class to a Child Class

    Hi

    can anyone give me a code example of how to inherit from a parent class to a child class.

    Example:

    Parent Class:

    CEmployee.vb
    Properties: EmpNo, FirstName, LastName, PhoneNo
    Methods: Pay, Report


    Child Classes:

    CPartTimeEmp.vb
    CFullTimeEmp.vb


    How can i inherit the properties and methods of the parent class (CEmployee) to the child classes (CPartTimeEmp, CFullTimeEmp) how should I start the child classes codes? What will the format be? Any example given will be much appreciated.. Thanks in advance...


  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I've customized this little example from one of my lengthy classes when I was training to do this kind of stuff (note : I included some explanation to these classes but you still have to read more about inheritance ) :
    VB Code:
    1. 'Parent Class
    2. Public Class Price
    3.  
    4.     'Conststans
    5.     Private Const Sandwich_1 As Decimal = 3.0
    6.     Private Const Sandwich_2 As Decimal = 3.5
    7.     Private Const Sandwich_3 As Decimal = 3.95
    8.     Private Const Sandwich_4 As Decimal = 4.5
    9.     Private Price As Decimal
    10.  
    11.  
    12.     'Child Class
    13.     Public Class Sandwiches
    14.         'We start inheritance by using this keyword.
    15.         'It allows us to integrate (use) the Parent'
    16.         'Class variables , properties , methods in
    17.         'this Child Class . You have also MustInherit
    18.         'directive which forces child to inherits Parent
    19.         'Class ..etc .See list for these keywords and
    20.         'discription for each one in the Help Files
    21.  
    22.         Inherits Price
    23.         Public Sandwich_ID As Decimal
    24.  
    25.         Public Enum Sandwiches_en
    26.             GLINDA = 1
    27.             THE_Wicked = 2
    28.             Hagatha = 3
    29.             Burt = 4
    30.         End Enum
    31.  
    32.         Public Function SandwichesFunction(ByVal Index As Sandwiches_en) As Decimal
    33.  
    34.             If Index = Sandwiches_en.GLINDA Then
    35.                 'See here : We are able to see the Private variables
    36.                 'of the Parent Class and we can Get or Set
    37.                 'its value , moreover all Methods, Properties
    38.                 'of the Parent Class no matter they are
    39.                 'Private of Public , they are still
    40.                 'accessible from anywhere in out Child
    41.                 'Class.This applies to the below code also
    42.                 Sandwich_ID = MyBase.Sandwich_1
    43.                 Return Sandwich_ID
    44.             ElseIf Index = Sandwiches_en.THE_Wicked Then
    45.                 Sandwich_ID = MyBase.Sandwich_2
    46.                 Return Sandwich_ID
    47.             ElseIf Index = Sandwiches_en.Hagatha Then
    48.                 Sandwich_ID = MyBase.Sandwich_3
    49.                 Return Sandwich_ID
    50.             ElseIf Index = Sandwiches_en.Burt Then
    51.                 Sandwich_ID = MyBase.Sandwich_4
    52.                 Return Sandwich_ID
    53.             End If
    54.  
    55.         End Function
    56.  
    57.     End Class
    58.  
    59.  
    60.  
    61. End Class

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    It seems like dnixons example would be better; a full time employee is a subset of an employee; a price is not a subset of a sandwich. In the second case it's more logical to simply have your Sandwich object contain a Price object.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by sunburnt
    It seems like dnixons example would be better; a full time employee is a subset of an employee; a price is not a subset of a sandwich. In the second case it's more logical to simply have your Sandwich object contain a Price object.
    I know , I said , this is small snap of my very lengthy classes and it's commented out that Price is 'Parent Class' and Sandwiches is 'Child Class' . This is point of this code just to expose the using some inheritance keywords . I think , it should fairly easy to figure out how these methods and properties work together . Keep in mind , one or two or even three examples won't be enough to fully understand Inheritance . He has to read more about it .

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