This example is given in the following website http://www.c-sharpcorner.com/Forums/Thread/156771/. Following code is in the program. new keyword is in question.
public new string ToString() //Tostring() overrides the Object class version.
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
}
If use new keyword only without using object obj = payingStudent; why we do not get object version (Student and ScholarshipStudent). Because there is no override keyword to override object version. Problem is highlighted.
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()
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
}
}
class ScholarshipStudent : Student
{
new public void SetCredits(int creditHours)
{
credits = creditHours;
}
}
/*
Student Megan has 15 credits
Student Luke has 15 credits
Student
*/
Loading
VulpesPosted Sep 6, 2012, 3:53 PM
If you just want it to print the class names, then you'll need to change these lines:
to these:
By doing so the virtual ToString() method inherited from System.Object will be called rather than the 'new' one contained in the classes themselves.
Posted Sep 6, 2012, 4:25 PM
Thank you very much for your comment Akkiraju Ivaturi
Akkiraju IvaturiPosted Sep 6, 2012, 4:05 PM
Posted Sep 6, 2012, 3:44 PM
/*
Student Megan has 15 credits
Scholarship Student Luke has 15 credits
*/
Not I expected one, that means
Student
ScholarshipStudent
VulpesPosted Sep 6, 2012, 3:18 PM
Posted Sep 6, 2012, 2:49 PM
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);
Console.WriteLine(payingStudent.ToString());
Console.WriteLine(freeStudent.ToString());
Console.ReadKey();
}
}
class Student
{
private string name;
protected int credits;
public void SetName(string name)
{
this.name = name;
}
public void SetCredits(int creditHours)
{
credits = creditHours;
}
public new string ToString()
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
}
}
class ScholarshipStudent
{
private string name;
protected int credits;
public void SetName(string name)
{
this.name = name;
}
public void SetCredits(int creditHours)
{
credits = creditHours;
}
public new string ToString()
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
}
}
/*
Student Megan has 15 credits
Student Luke has 15 credits
*/
VulpesPosted Sep 6, 2012, 1:53 PM
Console.WriteLine(freeStudent.ToString());
causes the ToString() method it inherits from the Student class to be called because the ScholarshipStudent class doesn't have one of its own.
This method hides the virtual ToString() method which Student inherits from System.Object and is not itself virtual.
So, the output refers to Luke as a Student, not a Scholarship student.
Posted Sep 6, 2012, 1:40 PM
Student
ScholarshipStudent
But the output is as follows
/*
Student Megan has 15 credits
Student Luke has 15 credits
*/
Please explain the reason for that
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);
Console.WriteLine(payingStudent.ToString());
Console.WriteLine(freeStudent.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()
{
string stuString = "Student " + name + " has " + credits + " credits";
return stuString;
}
}
class ScholarshipStudent : Student
{
new public void SetCredits(int creditHours)
{
credits = creditHours;
}
}
/*
Student Megan has 15 credits
Student Luke has 15 credits
*/
VulpesPosted Sep 6, 2012, 11:30 AM
Console.WriteLine(payingStudent.ToString());
works out payingStudent.ToString() by calling the Student class's 'new' ToString() method.
This line:
Console.WriteLine(freeStudent.ToString());
works out freeStudent.ToString() by moving up the inheritance chain until it gets to Student's 'new' ToString() method and then calling that, given that the Scholarship class doesn't have its own version of ToString().
Finally, this line:
Console.WriteLine(obj.ToString());
works out obj.ToString() using the Object class's virtual ToString() method which by default returns the type of the object to which 'obj' refers - namely Student. Notice that this virtual method is still inherited by Student even though it's hidden by its own non-virtual ToString() method.