class SchoolStudent : Student
{
// Private Fields
private int totalMarks;
private int totalObtainedMarks;
private double percentage;
// Public Constructors
public SchoolStudent()
{
Console.WriteLine("New school student created. Parameterless constructor called...");
}
public SchoolStudent(int regNum, string name, DateTime dob, int totalMark, int totalObtainedMarks) : base(regNum, name, dob)
{
this.totalMarks = totalMarks;
this.totalObtainedMarks = totalObtainedMarks;
Console.WriteLine("New school student created. Parameterless constructor called...");
}
// Public Properties
public int TotalMark
{
get { return totalMarks; }
set { totalMarks = value; }
}
public int TotalObtainedMarks
{
get { return TotalObtainedMarks; }
set { totalObtainedMarks= value; }
}
// Public Method
public double GetPercentage()
{
percentage = (double)totalObtainedMarks / TotalObtainedMarks * 100;
return percentage;
}
}
The SchoolStudent class inherits the Student class by using the colon operator.
The SchoolStudent class inherits all the members of the Student class. In addition, it also declares its own member; three private fields (totalMarks, totalObtainedMarks and percentage) with their corresponding properties, two constructors (one without parameters and one with parameters) and one instance method (GetPercentage()). For now, forget about the second (parameterized) constructor of our SchoolStudent class. Let us write our Test and Main() method.
class Test
{
schStd = new SchoolStudent();
schStd.Name = "Nitin Sir";
s static void Main(string [] args)
{
Student st = new Student(1,"Sachin Chaudhary",new DateTime(2013,09,03));
Console.WriteLine("Age of student,{0} is {1}\n",st.Name, st.GetAge());
SchoolStudentchStd.DateOfBirth = new DateTime(2013,09,3);
schStd.totalMarks = 500;
schStd.totalObtainedMarks = 476;
Console.WriteLine("Age of student, {0} is {1} got {2}% marks.", schStd.Name, schStd.GetAge(), schStd.GetPercentage());
}
}
In the Main() method, first we made an object of the Student class (st) and printed the name and age of student st, then we made an object of the SchoolStudent class (schStd), Since we used a parameterlessconstructor to instantiate schStd, we set the values of its fieldsthrough properties and then printed the name, age and percentage of SchoolStudent (schStd).
Note that we are able to access the properties Name and DateOfBirth (defined in Student class) because the SchoolStudent class is inherited from the Student class, thus inheriting the public properties too, When we execute the above program, we get the following output:
The output of the first two lines is as expected, but see the output when we created the SchoolStudent object. First the parameterless constructor of Student is called and then the constructor of SchoolStudent is called.
Constructor calls in Inheritance
When we instantiate the sub-class (SchoolStudent), the compiler first instantiates the base-class (Student) by calling one of its constructors and then calling the constructor of the sub-class. Suppose now we want to use the second constructor of the SchoolStudent class. For this, we need to comment the line with the base keyword in SchoolStudent's second construction declaration and make other changes like:
public SchoolStudent(int regNum, string name, DateTime dob, int totalMark, int totalObtainedMarks)
// : base(regNum, name, dob)
{
this.Name = name;
this.dateOfBirth = dob;
this.totalMarks = totalMarks;
this.totalObtainedMarks = totalObtainedMarks;
Console.WriteLine("New school student created. Parameterless constructor called...");
}
This constructor takes as parameters the fields defined by SchoolStudent and those defined by its base class (Student) and sets the values accordingly using the properties. Now, we need to change the Main method to:
static void Main(string [] args)
{
Student st = new Student(2,"Nitin Sir",new DateTime(1993,11,07),500,476);
Console.WriteLine("Age of student,{0},is {1}. {0} got {2}% marks.",schStd.Name, schStd.GetAge(), schStd.GetPercentage());
}
In the Main() method, we created an object of SchoolStudent using the parameterized constructor and then printed the name,age and percentage of the SchoolStudent.
The output of the program is:
Consider the constructor calls shown in the output, first the parameterless constructor of the base-class (Student) is called and then the parameterized constructor of the sub-class (SchoolStudent) class is called. It shown that the compiler creates an object of the base class before it instantiates the sub-class. Now, let us comment out the parameterless constructor of the Student class.
class Student
{
...
/*public Student()
{
Console.WriteLine("New student created. Parameterless constructor called...");
}*/
...
}
Now when we try to compile the program, we get the error: