views:

31

answers:

1

There are two classes Person and Employee alt text

when it mapped to c# code

public class Person
{
    private string Name;
}

public class Employee : Person
{
    private string Department;

    public string GetName()
    {
        return "Person Name";
    }
}

My question is where can i write the getters and setters for this private attributes.is it ok to write them with in the same Person and Employee classes if yes isn't there a problem with mapping? because methods are also with in the same class(GetName()) or do i have to use separate classes for writing getters and setters.i'm confused with this class diagram mapping with the code.Can any one resolve this for me??

+1  A: 

Firstly, I would recommend you to you the properties approach and not the getters / setters one.

My take:

public class Person {

    private string name;

    public string Name {
        get {
            return this.name;
        }
    }
}

public class Department {

    private int id;
    private string name;

    public int ID {
        get {
            return this.id;
        }
    }

    public string Name {
        get {
            return this.name;
        }
    }
}

public class Employee : Person {

    private Department department;

    public Department Department {
        get {
            return this.department;
        }
    }
}

Employee.Name returns the employee name which is being declared within the Person class.

thelost
Can i do the same for Department in Employee class?
chamara
See the update.
thelost
Thanks for the answer!
chamara