All the above Solutions do fair justice with the Topic. Let me put this with some technical example.
Lets declare a
"Person"
Class. In this class we have a method
"DescribeYourself"
, which is declared as a
"virtual"
, so that it's implementation can be
Overriden in it's Child Classes.
public class Person
{
private string PersonName;
public Person() { }
public Person(string personName)
{
PersonName = personName;
}
public virtual string DescribeYourself()
{
return "I am a Person. And my name is " + PersonName +".";
}
}
Now lets declare
"Student"
Class, inherited from
"Person"
Class. In this we have
Overrided implementation of method
"DescribeYourself"
.
public class Student: Person
{
private string StudentName;
public Student(string studentName)
{
StudentName = studentName;
}
public override string DescribeYourself()
{
return "I am a Student. And my name is " + StudentName + ".";
}
}
Lets declare one more Class
"Teacher"
, inherited from
"Person"
Class, In this also we have
Overrided implementation of method
"DescribeYourself"
.
public class Teacher : Person
{
private string TeacherName;
public Teacher(string teacherName)
{
TeacherName = teacherName;
}
public override string DescribeYourself()
{
return "I am a Teacher. And my name is " + TeacherName + ".";
}
}
Now lets see Output of different Instantiations.
1) Firstly lets declare object of Class
"Person"
, instantiated by the same Class.
string whoIAm = string.Empty;
Person someOne = null;
someOne = new Person("Claire");
whoIAm = someOne.DescribeYourself();
Result:
I am a Person. And my name is Claire.
2) Now instantiate object of Class
"Person"
by Class
"Student"
.
string whoIAm = string.Empty;
Person someOne = null;
someOne = new Student("Claire");
whoIAm = someOne.DescribeYourself();
Result:
I am a Student. And my name is Claire.
3) Lets instantiate object of Class
"Person"
by Class
"Teacher"
.
string whoIAm = string.Empty;
Person someOne = null;
someOne = new Teacher("Claire");
whoIAm = someOne.DescribeYourself();
Result:
I am a Teacher. And my name is Claire.