Maha

Maha

  • NA
  • 0
  • 326.1k

Run time

Nov 1 2012 1:10 PM
This example and explanation is given in the following website http://www.c-sharpcorner.com/Forums/Thread/156771/.

using System;

class DemoStudents4
{
public static void Main()
{
Student payingStudent = new Student();
ScholarshipStudent freeStudent = new ScholarshipStudent();

payingStudent.SetName("Megan");
payingStudent.SetCredits(15);

freeStudent.SetName("Luke");
freeStudent.SetCredits(15);

object obj = payingStudent;

Console.WriteLine(payingStudent.ToString());
Console.WriteLine(freeStudent.ToString());
Console.WriteLine(obj.ToString());

Console.ReadKey();
}
}

class Student
{
private string name;
protected int credits;

public void SetName(string name)
{
this.name = name;
}
public void SetCredits(int creditHours)//SetCredits in the child class as well
{
credits = creditHours;
}
public new string ToString() //Tostring() overrides the Object class version.
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
//OR
//return String.Format("Student {0} has {1} credits", name, credits);
}
}

class ScholarshipStudent : Student
{
new public void SetCredits(int creditHours)
{
credits = creditHours;
}
}


/*
Student Megan has 15 credits
Student Luke has 15 credits
Student
*/

We are now assigning the Student object to the Object variable 'obj' but when ToString() is called, it's Object's version which gets invoked, not Student's version. As you probably know, when it's not overridden, Object.ToString() returns by default the runtime type of the object which in this case is Student.

If you leave those lines in but change 'new' to 'override', then ToString() again becomes polymorphic and it's Student's version which gets called.

Similarly, ScholarshipStudent's SetCredits method hides Student's method of the same name, though in this case the latter is not virtual and so can't be overridden anyway.

Even though an int has been used when constructing the string, the fact remains that the result is a string and therefore agrees with the return type of the method. When you concatenate a string with an int then the latter's ToString() method is called implicitly which is why this apparent mixing of types is allowed.

How do we know that Object.ToString() returns by default the runtime type of the object, because variable type and object type is same.


Answers (5)