|
-
Sep 16th, 2003, 06:38 AM
#1
Thread Starter
New Member
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...
-
Sep 16th, 2003, 09:41 AM
#2
Sleep mode
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:
'Parent Class
Public Class Price
'Conststans
Private Const Sandwich_1 As Decimal = 3.0
Private Const Sandwich_2 As Decimal = 3.5
Private Const Sandwich_3 As Decimal = 3.95
Private Const Sandwich_4 As Decimal = 4.5
Private Price As Decimal
'Child Class
Public Class Sandwiches
'We start inheritance by using this keyword.
'It allows us to integrate (use) the Parent'
'Class variables , properties , methods in
'this Child Class . You have also MustInherit
'directive which forces child to inherits Parent
'Class ..etc .See list for these keywords and
'discription for each one in the Help Files
Inherits Price
Public Sandwich_ID As Decimal
Public Enum Sandwiches_en
GLINDA = 1
THE_Wicked = 2
Hagatha = 3
Burt = 4
End Enum
Public Function SandwichesFunction(ByVal Index As Sandwiches_en) As Decimal
If Index = Sandwiches_en.GLINDA Then
'See here : We are able to see the Private variables
'of the Parent Class and we can Get or Set
'its value , moreover all Methods, Properties
'of the Parent Class no matter they are
'Private of Public , they are still
'accessible from anywhere in out Child
'Class.This applies to the below code also
Sandwich_ID = MyBase.Sandwich_1
Return Sandwich_ID
ElseIf Index = Sandwiches_en.THE_Wicked Then
Sandwich_ID = MyBase.Sandwich_2
Return Sandwich_ID
ElseIf Index = Sandwiches_en.Hagatha Then
Sandwich_ID = MyBase.Sandwich_3
Return Sandwich_ID
ElseIf Index = Sandwiches_en.Burt Then
Sandwich_ID = MyBase.Sandwich_4
Return Sandwich_ID
End If
End Function
End Class
End Class
-
Sep 16th, 2003, 11:56 AM
#3
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.
-
Sep 16th, 2003, 12:51 PM
#4
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|