matthew phung
asked on
Question about passing inherited objects in methods
Hi experts,
I have a class called student that inherits from a person class. I wanted to update the student's name only so I created a method in my person repository called Update(Person p) that accepts a person.
public Person
{
public string Name;
}
public Student : Person
{
public string StudentID
}
....
Person Repository
{
public void Update(Person p)
}
xxx some where in my program xxx
Student student = Repository.GetStudent();
student.Name = "Jack";
Repository.Update(student) ;
I know that the Update method will contain a person object that I can interact with but HOW is the data being sent to the repository? Does the student object get sent to the repository or does .Net automatically extract the Person object from the Student object then sends it to the repository?
I have a class called student that inherits from a person class. I wanted to update the student's name only so I created a method in my person repository called Update(Person p) that accepts a person.
public Person
{
public string Name;
}
public Student : Person
{
public string StudentID
}
....
Person Repository
{
public void Update(Person p)
}
xxx some where in my program xxx
Student student = Repository.GetStudent();
student.Name = "Jack";
Repository.Update(student)
I know that the Update method will contain a person object that I can interact with but HOW is the data being sent to the repository? Does the student object get sent to the repository or does .Net automatically extract the Person object from the Student object then sends it to the repository?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Even though each automobile has a door that you can open, each door functions differently? I passed two different automobiles to you, and you still knew how to get into them. I didn't chunk out automobile functionality...I gave you the whole car and whole truck in each case. The same thing happens in your code. .NET doesn't do anything special with the object itself. Rather the whole object is passed, and the contract set forth by the base class gives .NET enough information to know that each person has a specific set of methods and propeties.