Different ways to invoke a hidden base class member from derived class
1. use base keyword
2. cast child type to parent type and invoke the hidden member
3. parentClass PC = new ChildClass()
PC.HiddenMethod()
======================
using System;
public class Employee
{
public string FirstName;
public string LastName;
public void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName);
}
}
public class FullTimeEmployee : Employee
{
//public void PrintFullName() //hiding - need to be avoided warning
public new void PrintFullName() //hiding intentionally
{
//base.PrintFullName(); // called method in base class
Console.WriteLine(FirstName + " " + LastName + " - Contractor");
}
}
public class PartTimeEmployee : Employee
{
}
public class Program
{
static void Main(string[] args)
{
//FullTimeEmployee FTE = new FullTimeEmployee();
//FullTimeEmployee FTE = new Employee(); // not allowe
Employee FTE = new FullTimeEmployee(); //called base method
FTE.FirstName = "Rody";
FTE.LastName = "Choi";
FTE.PrintFullName();
//((Employee)FTE).PrintFullName(); //called base method
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.FirstName = "Part";
PTE.LastName = "Time";
PTE.PrintFullName();
//((Employee)PTE).PrintFullName(); //called base method
}
}
========================
difference method overriding VS Method Hiding
using System;
public class BassClass
{
public virtual void Print()
{
Console.WriteLine("I am a Base Class Print Method");
}
}
//Method overriding
//calling drived class - overridden method in the Child class
//public class DerivedClass : BassClass
//{
// public override void Print()
// {
// Console.WriteLine("I am a Derived Class Print Method");
// }
//}
//Method hiding
// calling base class - hidden method in the base class
public class DerivedClass : BassClass
{
public new void Print()
{
Console.WriteLine("I am a Derived Class Print Method");
}
}
public class Program
{
public static void Main()
{
BassClass B = new DerivedClass();
B.Print();
DerivedClass D = new DerivedClass();
D.Print();
}
}