In Visual Studio, I have created a console application in which there is a class named “Student” that has the two fields FirstName and LastName and a method that prints the FirstName and LastName on the console screen.
- using System;
- namespace methodHidingInCSharp {
- class Student {
- public string FirstName;
- public string LastName;
- public void PrintName() {
- Console.WriteLine(FirstName + " " + LastName);
- }
- }
- class Program {
- static void Main(string[] args) {
- }
- }
- }
- using System;
- namespace methodHidingInCSharp {
- class Student {
- public string FirstName;
- public string LastName;
- public void PrintName() {
- Console.WriteLine(FirstName + " " + LastName);
- }
- }
- class DiplomaStudent : Student {
- }
- class GraduateStudent : Student {
- }
- class Program {
- static void Main(string[] args) {
- }
- }
- }
- class Program {
- static void Main(string[] args) {
- DiplomaStudent ds = new DiplomaStudent();
- ds.FirstName = "Sam";
- ds.LastName = "Fisher";
- ds.PrintName();
- GraduateStudent gs = new GraduateStudent();
- gs.FirstName = "Aiden";
- gs.LastName = "Pearce";
- gs.PrintName();
- }
- }
Since we are inheriting the Diploma and Graduate Student classes from the Student class, all the members except the private members can be called.
If we run the project, we will get the output as:

So, we have the output we are expecting. But there is a slight problem. We don't know out of these two which one is the diplomaStudent and which one is the graduateStudent and to overcome this problem I can add a (– studentType) appended to his name stating diploma or graduate student. With which by just looking at the names we can determine which one is a diploma student and which one is the graduate student.
Let's flip back to our project. Now what I will do is to create a PrintName method that is the same in both of the derived classes and I will append a diploma student and a graduate student to make it more readable and clear.
- class DiplomaStudent : Student {
- public void PrintName() {
- Console.WriteLine(FirstName + " " + LastName + " - Diploma Student");
- }
- }
- class GraduateStudent : Student {
- public void PrintName() {
- Console.WriteLine(FirstName + " " + LastName+ " - Graduate Student");
- }
- }
- class Student {
- public string FirstName;
- public string LastName;
- public void PrintName() {
- Console.WriteLine(FirstName + " " + LastName);
- }
- }
But when we build our solution, we see a Green squiggly line and if we hover the mouse on it, it indicates that if you where intentionally hiding the inherited member then use the new keyword.

To hide an inherited member from the derived class, use the new keyword as in the following:
- class DiplomaStudent : Student {
- public new void PrintName() {
- Console.WriteLine(FirstName + " " + LastName + " - Diploma Student");
- }
- }
- class GraduateStudent : Student {
- public new void PrintName() {
- Console.WriteLine(FirstName + " " + LastName+ " - Graduate Student");
- }
- }

So, we have accomplished what we wanted. Now let's see how to invoke it back after hiding it.
We can do it in three ways.
The first one is to use the base keyword.
- class DiplomaStudent : Student {
- public new void PrintName() {
- base.PrintName();
- }
- }
- class GraduateStudent : Student {
- public new void PrintName() {
- Console.WriteLine(FirstName + " " + LastName+ " - Graduate Student");
- }

The second way to do this is to type-caste the child class variable to the base class.
- using System;
- namespace methodHidingInCSharp {
- class DiplomaStudent : Student {
- public new void PrintName() {
- Console.WriteLine(FirstName + " " + LastName + " - Diploma Student");
- }
- }
- class GraduateStudent : Student {
- public new void PrintName() {
- Console.WriteLine(FirstName + " " + LastName+ " - Graduate Student");
- }
- }
- class Program {
- static void Main(string[] args) {
- DiplomaStudent ds = new DiplomaStudent();
- ds.FirstName = "Sam";
- ds.LastName = "Fisher";
- re I have type-casted the child class reference variable into a base class
- ((Student)ds).PrintName();
- GraduateStudent gs = new GraduateStudent();
- gs.FirstName = "Aiden";
- gs.LastName = "Pearce";
- ((Student)gs).PrintName();
- }
- }

In the preceding output, we have the base implementation.
The third way is to create a base class reference variable pointing to the child class.
- class Program {
- static void Main(string[] args) {
- Student ds = new DiplomaStudent();
- ds.FirstName = "Sam";
- ds.LastName = "Fisher";
- ds.PrintName();
- Student gs = new GraduateStudent();
- gs.FirstName = "Aiden";
- gs.LastName = "Pearce";
- gs.PrintName();
- }
- }

No comments:
Post a Comment