inheritance question

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

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
 
Hi,

When you derive a class A from class B,
from class B, you can access public and protected members of class A.

Here in your sample, you don't have any public,protected members in your
base class.

So though you inherit from a class, it doesn't add any value.

So make the private members of your base class (mDOB, mAddress,
mMaritalStatus ) into protected members.
You can now discard the duplicate copy of those members in your derived
class.

Hope this helps.

Regards,
R.Balaji
 
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.
 
Excellent Dmitriy, that was exactly the sort of explanation I was
looking for. I'm now beginning to understand how to implement
inheritance a bit better. Cheers!
 
Back
Top