TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
600
70.8k
New & override
Jan 4 2015 10:18 AM
It is said that when you use '
new
' the ToString() method in the base class (System.Object) is hidden, whereas if you use '
override
' it's overridden. But in this program whether you use
new
or
override
the result is same. I wish to know whether it is possible to explain the reason without inserting new lines in the program. Problem is highlighted.
This program is explained by inserting two new lines in the following webpage (
http://www.c-sharpcorner.com/Forums/Thread/156771/
) but I wish to know whether it is possible to explain without inserting new lines.
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() //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
*/
Reply
Answers (
2
)
Adding multiple images to the database
error: Microsoft Office Access database engine cannot open