473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

OOP Inheritance

RSH
Being rather new to OOP constructs I have a question regarding inheritance.

What is the proper way to setup my classes in a Company/Employee
relationship.

I have a Company Object and a company has employees (one to many
relationship) and CompanyDeductions. The Employees have EmployeeDeductions.
Based on my limited knowledge I set my objects as follows:

Public Class Company

Private _CompanyID As String

Private _Employees As New ArrayList

Private _Deductions As New ArrayList

Public Property CompanyID() As String

Get

Return _CompanyID

End Get

Set(ByVal Value As String)

_CompanyID = Value

End Set

End Property

Public ReadOnly Property Employees() As ArrayList

Get

Return _Employees

End Get

End Property

Public Sub LoadEmployees()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployee As New employee(oDR("EmployeeID"))

_Employees.Add(oEmployee)

End Sub

Public Sub LoadCompanyDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oCompanyDeduction As New CompanyDeduction(oDR("DeductionID"))

_Employees.Add(oCompanyDeduction)

End Sub

End Class

Public Class Employee

Inherits Company

Private _EmployeeID As String

Private _FirstName As String

Private _LastName As String

Private _Deductions As ArrayList

Public Sub New(ByVal EmployeeID As String)

_EmployeeID = EmployeeID

End Sub

Public ReadOnly Property EmployeeID() As String

Get

Return _EmployeeID

End Get

End Property

Public ReadOnly Property FirstName() As String

Get

Return _FirstName

End Get

End Property

Public ReadOnly Property LastName() As String

Get

Return _LastName

End Get

End Property

Public Sub LoadEmployeeDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployeeDeduction As New EmployeeDeduction(oDR("DeductionID"))

_Deductions.Add(oEmployeeDeduction)

End Sub

End Class

Public Class CompanyDeduction

Inherits Company

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Public Class EmployeeDeduction

Inherits Employee

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Is this a good strategy? Or how could I make it better 9or more industry
standard?

Thank you for your time.

Ron
Mar 29 '07 #1
8 920
Ron,

No, this is not a good strategy.

An employee is not a company. A deduction is not an employee. Inheritance
really doesn't come into play in this example.

Kerry Moorman
"RSH" wrote:
Being rather new to OOP constructs I have a question regarding inheritance.

What is the proper way to setup my classes in a Company/Employee
relationship.

I have a Company Object and a company has employees (one to many
relationship) and CompanyDeductions. The Employees have EmployeeDeductions.
Based on my limited knowledge I set my objects as follows:

Public Class Company

Private _CompanyID As String

Private _Employees As New ArrayList

Private _Deductions As New ArrayList

Public Property CompanyID() As String

Get

Return _CompanyID

End Get

Set(ByVal Value As String)

_CompanyID = Value

End Set

End Property

Public ReadOnly Property Employees() As ArrayList

Get

Return _Employees

End Get

End Property

Public Sub LoadEmployees()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployee As New employee(oDR("EmployeeID"))

_Employees.Add(oEmployee)

End Sub

Public Sub LoadCompanyDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oCompanyDeduction As New CompanyDeduction(oDR("DeductionID"))

_Employees.Add(oCompanyDeduction)

End Sub

End Class

Public Class Employee

Inherits Company

Private _EmployeeID As String

Private _FirstName As String

Private _LastName As String

Private _Deductions As ArrayList

Public Sub New(ByVal EmployeeID As String)

_EmployeeID = EmployeeID

End Sub

Public ReadOnly Property EmployeeID() As String

Get

Return _EmployeeID

End Get

End Property

Public ReadOnly Property FirstName() As String

Get

Return _FirstName

End Get

End Property

Public ReadOnly Property LastName() As String

Get

Return _LastName

End Get

End Property

Public Sub LoadEmployeeDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployeeDeduction As New EmployeeDeduction(oDR("DeductionID"))

_Deductions.Add(oEmployeeDeduction)

End Sub

End Class

Public Class CompanyDeduction

Inherits Company

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Public Class EmployeeDeduction

Inherits Employee

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Is this a good strategy? Or how could I make it better 9or more industry
standard?

Thank you for your time.

Ron
Mar 29 '07 #2
RSH
So I should just treat them all as individual objects and instantiate them
from with in the other objects?
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:C7**********************************@microsof t.com...
Ron,

No, this is not a good strategy.

An employee is not a company. A deduction is not an employee. Inheritance
really doesn't come into play in this example.

Kerry Moorman
"RSH" wrote:
>Being rather new to OOP constructs I have a question regarding
inheritance.

What is the proper way to setup my classes in a Company/Employee
relationship.

I have a Company Object and a company has employees (one to many
relationship) and CompanyDeductions. The Employees have
EmployeeDeductions.
Based on my limited knowledge I set my objects as follows:

Public Class Company

Private _CompanyID As String

Private _Employees As New ArrayList

Private _Deductions As New ArrayList

Public Property CompanyID() As String

Get

Return _CompanyID

End Get

Set(ByVal Value As String)

_CompanyID = Value

End Set

End Property

Public ReadOnly Property Employees() As ArrayList

Get

Return _Employees

End Get

End Property

Public Sub LoadEmployees()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployee As New employee(oDR("EmployeeID"))

_Employees.Add(oEmployee)

End Sub

Public Sub LoadCompanyDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oCompanyDeduction As New CompanyDeduction(oDR("DeductionID"))

_Employees.Add(oCompanyDeduction)

End Sub

End Class

Public Class Employee

Inherits Company

Private _EmployeeID As String

Private _FirstName As String

Private _LastName As String

Private _Deductions As ArrayList

Public Sub New(ByVal EmployeeID As String)

_EmployeeID = EmployeeID

End Sub

Public ReadOnly Property EmployeeID() As String

Get

Return _EmployeeID

End Get

End Property

Public ReadOnly Property FirstName() As String

Get

Return _FirstName

End Get

End Property

Public ReadOnly Property LastName() As String

Get

Return _LastName

End Get

End Property

Public Sub LoadEmployeeDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployeeDeduction As New EmployeeDeduction(oDR("DeductionID"))

_Deductions.Add(oEmployeeDeduction)

End Sub

End Class

Public Class CompanyDeduction

Inherits Company

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Public Class EmployeeDeduction

Inherits Employee

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Is this a good strategy? Or how could I make it better 9or more industry
standard?

Thank you for your time.

Ron

Mar 29 '07 #3
RSH,

A company has a collection of 0 or more employee's

The empoyee's are a property for the company, but a class itself which can
inherit female and male employees.

Cor
"RSH" <wa*************@yahoo.comschreef in bericht
news:OO**************@TK2MSFTNGP02.phx.gbl...
So I should just treat them all as individual objects and instantiate them
from with in the other objects?
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:C7**********************************@microsof t.com...
>Ron,

No, this is not a good strategy.

An employee is not a company. A deduction is not an employee. Inheritance
really doesn't come into play in this example.

Kerry Moorman
"RSH" wrote:
>>Being rather new to OOP constructs I have a question regarding
inheritance.

What is the proper way to setup my classes in a Company/Employee
relationship.

I have a Company Object and a company has employees (one to many
relationship) and CompanyDeductions. The Employees have
EmployeeDeductions.
Based on my limited knowledge I set my objects as follows:

Public Class Company

Private _CompanyID As String

Private _Employees As New ArrayList

Private _Deductions As New ArrayList

Public Property CompanyID() As String

Get

Return _CompanyID

End Get

Set(ByVal Value As String)

_CompanyID = Value

End Set

End Property

Public ReadOnly Property Employees() As ArrayList

Get

Return _Employees

End Get

End Property

Public Sub LoadEmployees()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployee As New employee(oDR("EmployeeID"))

_Employees.Add(oEmployee)

End Sub

Public Sub LoadCompanyDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oCompanyDeduction As New CompanyDeduction(oDR("DeductionID"))

_Employees.Add(oCompanyDeduction)

End Sub

End Class

Public Class Employee

Inherits Company

Private _EmployeeID As String

Private _FirstName As String

Private _LastName As String

Private _Deductions As ArrayList

Public Sub New(ByVal EmployeeID As String)

_EmployeeID = EmployeeID

End Sub

Public ReadOnly Property EmployeeID() As String

Get

Return _EmployeeID

End Get

End Property

Public ReadOnly Property FirstName() As String

Get

Return _FirstName

End Get

End Property

Public ReadOnly Property LastName() As String

Get

Return _LastName

End Get

End Property

Public Sub LoadEmployeeDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployeeDeduction As New EmployeeDeduction(oDR("DeductionID"))

_Deductions.Add(oEmployeeDeduction)

End Sub

End Class

Public Class CompanyDeduction

Inherits Company

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Public Class EmployeeDeduction

Inherits Employee

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Is this a good strategy? Or how could I make it better 9or more
industry
standard?

Thank you for your time.

Ron


Mar 29 '07 #4
RSH,
>
A company has a collection of 0 or more employee's
Agreed
>
The empoyee's are a property for the company, but a class itself which
can inherit female and male employees.
I'd have said either...
....'Gender' is a property of 'Employee'
Or
....'Male' and 'Female' are classes which inherit from 'Employee'

I think though that the first option is better because a 'Gender' property
could be pulled back into an ancesstor class 'Person' which 'Employee' and
'Owner' and 'Manager' could all inherit from.

Just abnother 2c

--
Rory

Mar 29 '07 #5
RSH
Thanks that makes perfect sense.

What collection is best for storing objects in the scenerio you cite below?
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
RSH,

A company has a collection of 0 or more employee's

The empoyee's are a property for the company, but a class itself which can
inherit female and male employees.

Cor
"RSH" <wa*************@yahoo.comschreef in bericht
news:OO**************@TK2MSFTNGP02.phx.gbl...
>So I should just treat them all as individual objects and instantiate
them from with in the other objects?
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:C7**********************************@microso ft.com...
>>Ron,

No, this is not a good strategy.

An employee is not a company. A deduction is not an employee.
Inheritance
really doesn't come into play in this example.

Kerry Moorman
"RSH" wrote:

Being rather new to OOP constructs I have a question regarding
inheritance.

What is the proper way to setup my classes in a Company/Employee
relationship.

I have a Company Object and a company has employees (one to many
relationship) and CompanyDeductions. The Employees have
EmployeeDeductions.
Based on my limited knowledge I set my objects as follows:

Public Class Company

Private _CompanyID As String

Private _Employees As New ArrayList

Private _Deductions As New ArrayList

Public Property CompanyID() As String

Get

Return _CompanyID

End Get

Set(ByVal Value As String)

_CompanyID = Value

End Set

End Property

Public ReadOnly Property Employees() As ArrayList

Get

Return _Employees

End Get

End Property

Public Sub LoadEmployees()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployee As New employee(oDR("EmployeeID"))

_Employees.Add(oEmployee)

End Sub

Public Sub LoadCompanyDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oCompanyDeduction As New CompanyDeduction(oDR("DeductionID"))

_Employees.Add(oCompanyDeduction)

End Sub

End Class

Public Class Employee

Inherits Company

Private _EmployeeID As String

Private _FirstName As String

Private _LastName As String

Private _Deductions As ArrayList

Public Sub New(ByVal EmployeeID As String)

_EmployeeID = EmployeeID

End Sub

Public ReadOnly Property EmployeeID() As String

Get

Return _EmployeeID

End Get

End Property

Public ReadOnly Property FirstName() As String

Get

Return _FirstName

End Get

End Property

Public ReadOnly Property LastName() As String

Get

Return _LastName

End Get

End Property

Public Sub LoadEmployeeDeductions()

' Database call to get employees

Dim oDR As SqlClient.SqlDataReader

Dim oEmployeeDeduction As New EmployeeDeduction(oDR("DeductionID"))

_Deductions.Add(oEmployeeDeduction)

End Sub

End Class

Public Class CompanyDeduction

Inherits Company

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Public Class EmployeeDeduction

Inherits Employee

Private _DeductionID As String

Private _DeductionName As String

Public Sub New(ByVal DeductionID As String)

_DeductionID = DeductionID

End Sub

Public ReadOnly Property DeductionID() As String

Get

Return _DeductionID

End Get

End Property

Public ReadOnly Property DeductionName() As String

Get

Return _DeductionName

End Get

End Property

End Class

Is this a good strategy? Or how could I make it better 9or more
industry
standard?

Thank you for your time.

Ron



Mar 29 '07 #6
Rory,

"Rory Becker" <Ro********@newsgroup.nospamschrieb:
>The empoyee's are a property for the company, but a class itself which
can inherit female and male employees.

I'd have said either...
...'Gender' is a property of 'Employee'
Or
...'Male' and 'Female' are classes which inherit from 'Employee'

I think though that the first option is better because a 'Gender' property
could be pulled back into an ancesstor class 'Person' which 'Employee' and
'Owner' and 'Manager' could all inherit from.
I'd implement the role pattern instead of using subtyping for different
genders and positions.

Martin Fowler: Dealing With Roles
<URL:http://www.martinfowler.com/apsupp/roles.pdf>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 29 '07 #7
I'll offer the opinion that it doesn't make perfect sense :-)

Would temporary employees and full-time also be subclasses? Are there then
subclasses of FemaleTemporary and MaleTemporary? How about former employees
and retirees who still get pension checks, more subclasses? Does a company
class have another collection for Consultants?

So imagine the app has been written and you're entering a new employee... is
the code instantiating a new FemaleEmployee() or MaleEmployee() or does it
have to ask that question first? Have you ever seen an app ask the sex of
the individual first? Let's say you need to fetch a person out of the
database do you instantiate an employee by passing a social security number?
And what happens if that person is a consultant? Wouldn't the datatype of
the variable tend to be wrong? You'd have to know at the time the SSN was
keyed in which type of object you expected back when all the user wants is a
person's personnel record.

Food for thought...
"RSH" <wa*************@yahoo.comwrote...
Thanks that makes perfect sense.

What collection is best for storing objects in the scenerio you cite
below?
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>RSH,

A company has a collection of 0 or more employee's

The empoyee's are a property for the company, but a class itself which
can inherit female and male employees.

Cor


Mar 30 '07 #8
>Have you ever seen an app ask the sex of the individual first?

Plenty of, just visit internet for dating sites although I have plenty of
more samples, but they are of course all sex (can be medical) related..

Cor
Mar 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.