6 Replies - 1682 Views - Last Post: 28 July 2010 - 10:01 AM Rate Topic: -----

#1 Searock  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 259
  • Joined: 15-September 09

Best pratice in Inheritance

Posted 27 July 2010 - 12:47 PM

Hi,

I have three classes Employee, Manager and Salesman. Manager and Salesman classes inherits Employee class.

Employee :

Public MustInherit Class Employee
    ' Field data.
    Protected empName As String
    Protected empID As Integer
    Protected currPay As Single

    ' Constructors
    Public Sub New()
    End Sub

    Public Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single)
        empName = name
        empID = id
        currPay = pay
    End Sub
End Class



Manager :

Public Class Manager
    Inherits Employee

    Private Dept As String
    Sub New()

    End Sub

    Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single, ByVal dept As String)
        MyBase.New(name, id, pay)
        Me.Dept = dept
    End Sub

End Class



Salesman :

Public Class Salesman
    Inherits Employee

    Private noOfSales As Integer

    Sub New()

    End Sub

    Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single, ByVal sales As Integer)
        MyBase.New(name, id, pay)
        Me.noOfSales = sales
    End Sub

End Class



Now I have created a object of salesman and manager using the following code.

Dim objSalesMan as Employee=new Salesman("xyz",1,2000,5000)
Dim objManager as Employee=new Manager("abx",2,5000,"production")



Is this a good programming pratice or should I use

Dim objSalesMan as new Salesman("xyz",1,2000,5000)
Dim objManager as new Manager("abx",2,5000,"production")



I am confused. Can someone point me in a right direction ?

Thanks.

This post has been edited by Searock: 27 July 2010 - 12:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Best pratice in Inheritance

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,060
  • Joined: 02-July 08

Re: Best pratice in Inheritance

Posted 27 July 2010 - 08:40 PM

Well from the little I play with inheritance, here goes...

Either will work, I use the second way. You can pass either object where the parameter is declared as Employee, because they in fact are the same Base-Type, yet can function differently. There's probably some fine detail about how these declarations are made, but they both work the same. Think of "is A" with inheritance, a Manager "is A" Employee.

HTH
Was This Post Helpful? 1
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best pratice in Inheritance

Posted 27 July 2010 - 09:59 PM

I think the route I would go would be an interface type design structure. You could have an interface, name it IEMployee, then Salesman and Manager can implement it. So your IEmployee inteface could look like this (or similar)

Public Interface IEmployee
    Property empName() As String
    Property empID() As Integer
    Property currPay() As Single
    Property Dept() As String
    Property NoOfSales() As Integer
End Interface



Then your classes could implement your interface. Here's an example of your Manager class

Public Class Manager
    Implements IEmployee
    Public Property empName() As String Implements IEmployee.empName

    Public Property empID() As Integer Implements IEmployee.empID

    Public Property currPay() As Single Implements IEmployee.currPay

    Public Property Dept() As String Implements IEmployee.Dept

    Public Property NoOfSales() As Integer Implements IEmployee.NoOfSales
        Get
            Throw New NotImplementedException("noOFSales property is not implemented in the Manager class")
        End Get
        Set(ByVal value As Integer)
            Throw New NotImplementedException("noOFSales property is not implemented in the Manager class")
        End Set
    End Property
End Class



Notice how we throw a NotImplementedException with the NoOfSales Property, we did that because this class doesnt implement that property. Such as a Salesman class would throw the same exception on the Dept property like this

Public Class Salesman
    Implements IEmployee
    Public Property empName() As String Implements IEmployee.empName

    Public Property empID() As Integer Implements IEmployee.empID

    Public Property currPay() As Single Implements IEmployee.currPay

    Public Property Dept() As String Implements IEmployee.Dept
        Get
            Throw New NotImplementedException("Dept property is not implemented in the Salesman class")
        End Get
        Set(ByVal value As String)
            Throw New NotImplementedException("Dept property is not implemented in the Salesman class")
        End Set
    End Property

    Public Property noOFSales() As Integer Implements IEmployee.NoOfSales
End Class



Then as you grow your application, most classes (employee, manager, etc) can have same methods implemented differently as long as they're listed in your main Interface.

Hope that makes sense :)
Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,416
  • Joined: 29-May 08

Re: Best pratice in Inheritance

Posted 27 July 2010 - 10:52 PM

PsychoCoder I would go with the first option rather then yours as I consider Interfaces to be contracts. If the details on the interface changes (for example a new property) all of the implementers of that interface are now broken.
Was This Post Helpful? 1
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best pratice in Inheritance

Posted 27 July 2010 - 10:57 PM

I guess it's personal choice. I always go with interface design pattern over most others, if you know things going in then it's impossible to really break it since you know it's coming. Just my personal preference
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,416
  • Joined: 29-May 08

Re: Best pratice in Inheritance

Posted 27 July 2010 - 11:23 PM

View PostPsychoCoder, on 28 July 2010 - 05:57 AM, said:

I guess it's personal choice. I always go with interface design pattern over most others, if you know things going in then it's impossible to really break it since you know it's coming. Just my personal preference

True, which case I use inheritance on the interfaces too.

Public Interface IEmployee
  ReadOnly Property empName() As String
  ReadOnly Property empID() As Integer
  ReadOnly Property currPay() As Single
End Interface


Public Interface IManager
  Inherits IEmployee
  ReadOnly Property Dept() As String
End Interface


Public Interface ISalesPerson
  Inherits IEmployee
  ReadOnly Property NoOfSales() As Integer

End Interface



And then implement them on the inherited classes
Public MustInherit Class Base_Employee
  Implements IEmployee
  Protected Friend _empName As String
  Protected Friend _empID As Integer
  Protected Friend _currPay As Single
  Public ReadOnly Property Name As String Implements IEmployee.empName
    Get
      Return _empName
    End Get
  End Property
  Public ReadOnly Property ID As Integer Implements IEmployee.empID
    Get
      Return _empID
    End Get
  End Property
  Public ReadOnly Property Pay As Single Implements IEmployee.currPay
    Get
      Return _currPay
    End Get
  End Property
  Public Overrides Function ToString() As String
    Return String.Format("{0} {1} {2}", Name, ID, Pay)
  End Function
End Class


Public Class Employee
  Inherits Base_Employee
  Public Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single)
    _empName = name
    _empID = id
    _currPay = pay
  End Sub
End Class


The IEmployee is implemented by the base class, so doesn't need implementing.

Public Class Manager
  Inherits Employee
  Implements IManager
  Protected Friend _Dept As String
  Public ReadOnly Property Dept As String Implements IManager.Dept
    Get
      Return _Dept
    End Get
  End Property
  Public Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single, ByVal dept As String)
    MyBase.New(name, id, pay)
    _Dept = dept
  End Sub
  Public Overrides Function ToString() As String
    Return String.Format("{0} {1}", MyBase.ToString(), _Dept)
  End Function
End Class



Public Class Salesman
  Inherits Employee
  Implements ISalesPerson
  Protected Friend _noOfSales As Integer
  Public ReadOnly Property noOfSales As Integer Implements ISalesPerson.NoOfSales
    Get
      Return _noOfSales
    End Get
  End Property
  Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single, ByVal sales As Integer)
    MyBase.New(name, id, pay)
    _noOfSales = sales
  End Sub
  Public Overrides Function ToString() As String
    Return String.Format("{0} {1}", MyBase.ToString(), _noOfSales)
  End Function
End Class




Now Suppose we want a SaleManager that is both a manager and a SalesPerson
Public Interface ISalesManager
  Inherits IManager, ISalesPerson
End Interface


Since in .net we can't have multiple inheritance on classes
Public Class SalesManager
  Inherits Manager
  Implements ISalesManager
  Protected Friend _Sales As Integer
  Public Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single, ByVal dept As String, ByVal NoSales As Integer)
    MyBase.New(name, id, pay, dept)
    _Sales = NoSales
  End Sub
  Public ReadOnly Property NoOfSales As Integer Implements ISalesPerson.NoOfSales
    Get
      Return _Sales
    End Get
  End Property
End Class


Was This Post Helpful? 3
  • +
  • -

#7 Searock  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 259
  • Joined: 15-September 09

Re: Best pratice in Inheritance

Posted 28 July 2010 - 10:01 AM

, AdamSpeight2008, PsychoCoder thanks for your replies :genius: .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1