Hi All,
I have gone through all articles in this community.
Can anyone practically explain the difference between method hiding/overriding in OOPS.
Theoritically got idea from this community queries/Answers. But need practically.
Thanks in Advance,
Sree
satheesh dPosted Oct 29, 2021, 11:27 AM
MethodHiding
class Employee
{
public virtual void print()
{
Console.WriteLine("Employee");
}
}
class FullTimeEmployee:Employee
{
public new void print()
{
Console.WriteLine("Full Time");
}
}
static void Main()
{
FullTimeEmployee FTE = new FullTimeEmployee();
FTE.print();
}
Here the Output will be “Full Time”. The Fulltime Employee class is inherited from Employee class. We created the instance of class with Fulltime Employee class, So the print method inside this class will be executed. This class hides the Employee class print method. This is called method hiding. If the hiding is intended then use new keyword is used in the print method.
Method Overriding
class Employee
{
public virtual void print()
{
Console.WriteLine("Employee");
}
}
class FullTimeEmployee:Employee
{
public Override void print()
{
Console.WriteLine("Full Time");
}
}
This is the example for method overriding. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.
Satya KarkiPosted Oct 19, 2021, 12:38 PM
Rijwan AnsariPosted Oct 19, 2021, 12:29 PM
Srinivasan RamamoorthiPosted Oct 19, 2021, 10:29 AM
Sachin SinghPosted Oct 19, 2021, 9:00 AM