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
Rijwan AnsariRank #82 · 24.2K reputationPosted Oct 19, 2021, 8:29 AM EDT
Satya KarkiRank #78 · 24.6K reputationPosted Oct 19, 2021, 8:38 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Oct 19, 2021, 5:00 AM EDT
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Oct 19, 2021, 6:29 AM EDT
satheesh d190 reputationPosted Oct 29, 2021, 7:27 AM EDT
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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts