Re: inheritance question

From: Dmitriy Lapshin [C# / .NET MVP] (x-code_at_no-spam-please.hotpop.com)
Date: 03/17/04


Date: Wed, 17 Mar 2004 12:50:43 +0200

Hi Mike,

1. The Person class already has fields for DOB, address and marital status.
Since Employee extends Person, it would be logical to allow the inherited
class to have access to these fiedls. This is done by declaring the fields
as "protected" as opposed to "private".

2. Then, the constructor of the Employee class should accept all parameters
required by the Person constructor PLUS additional arguments such as
Department, NI Number and Position. Then, it should cast a magic spell
called "code reuse":

public Employee
{
  // Private declarations omitted for brevity

  public Employee (DateTime DOB, string Address, string MaritalStatus,
    string Department, string NINumber, string Position):
    base(DOB, Address, MaritalStatus)
  {
    mDepartment = Department;
    // The rest of the Employee-related initializations.
  }
}

So you have been on almost the right track, you just shouldn't duplicate the
fields of Person used by both of the classes in the Employee class
declaration.

-- 
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Mike P" <mrp@telcoelectronics.co.uk> wrote in message
news:%234bitOADEHA.624@TK2MSFTNGP10.phx.gbl...
> I'm trying to write my first project using inheritance, and I'm not sure
> whether I'm going about it the right way (excuse me if my terminology
> isn't quite right).
>
> If I have a class called Person which is used to set variables DOB,
> Address, MaritalStatus, and then Employee which inherits the Person
> class and extends it with Department, NI Number and Position, I'm not
> clear on how to instantiate these classes or what to put in the
> constructors.
>
> This is my code where I am instantiating them :
>
> Person clsPerson = new Person(DOB, Address, MaritalStatus);
>
> Employee clsEmployee = new Employee(Department, NINumber, Position);
>
> This is the beginning of my Person class :
>
> public class Person
> {
> private DateTime mDOB;
> private string mAddress;
> private string mMaritalStatus;
>
> public Person(DateTime DOB, string Address, string MaritalStatus)
> {
> mDOB = DOB;
> mAddress = Address;
> mMaritalStatus = MaritalStatus;
> }
>
> And this is the beginning of my Employee class :
>
> public class Employee : Person
> {
> private DateTime mDOB;
> private string mAddress;
> private string mMaritalStatus;
> private string mDepartment;
> private string mNINumber;
> private string mPosition;
>
> public Employee(string Department, string NINumber, string Position) :
> base(DOB, Address, MaritalStatus)
> {
> mDepartment = Department;
> mNINumber = NINumber;
> mPosition = Position;
> }
>
>
> Can somebody please tell me where I'm going wrong with this?
>
>
> Cheers,
>
> Mike
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!


Relevant Pages

  • Problem with Datagrid and OOP
    ... I have two different objects Employee and SalaryType ... public int Add ... private string _code; ...
    (microsoft.public.dotnet.languages.csharp)
  • Problem with Datagrid and OOP
    ... I have two different objects Employee and SalaryType ... public int Add ... private string _code; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: nullPointerException in abstract class
    ... | private String firstName; ... | public Employee(String first, String last, String ssn, String ...
    (comp.lang.java.programmer)
  • Re: nullPointerException in abstract class
    ... private String firstName; ... public Employee(String first, String last, String ssn, String ...
    (comp.lang.java.programmer)
  • Re: Gui Question
    ... employee they wish to create and specify the data concerning that ... private String firstName; ... public class CommissionEmployee extends Employee ... public class BasePlusCommissionEmployee extends CommissionEmployee ...
    (comp.lang.java.help)