Re: inheritance question
From: Dmitriy Lapshin [C# / .NET MVP] (x-code_at_no-spam-please.hotpop.com)
Date: 03/17/04
- Next message: Mark Rae: "Re: Simulating the File Properties dialog"
- Previous message: R.Balaji: "Re: Naming Advice Required (Re-Worded)"
- In reply to: Mike P: "inheritance question"
- Next in thread: Mike P: "Re: inheritance question"
- Reply: Mike P: "Re: inheritance question"
- Messages sorted by: [ date ] [ thread ]
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!
- Next message: Mark Rae: "Re: Simulating the File Properties dialog"
- Previous message: R.Balaji: "Re: Naming Advice Required (Re-Worded)"
- In reply to: Mike P: "inheritance question"
- Next in thread: Mike P: "Re: inheritance question"
- Reply: Mike P: "Re: inheritance question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|