We can predefine the method of parent class in child class by implementing the concept of method hiding. In method hiding the base(parent) class method is predefined in child class by specify new keyword.
Example:-
There are some steps to execute the method hiding program .
Step1:- Open visual studio->click File->New Project->Select Console Application-> give your application name below(method hiding).
See it:-
Step2:- Copy the above code and paste program.cs File.
See it:-
Step3:- Run the program(press F5).
See it:-
I hope this is useful for you.
For More:-
click below for download whole application.
DOWNLOAD
Example:-
using System;
namespace method_hiding
{
class Program
{
static void Main(string[] args)
{
//employee obj = new employee();
cls obj1 = new employee(); //up casting
obj1.display();
//obj.display();
Console.ReadLine();
}
}
public class cls
{
public void display()
{
Console.WriteLine("hello");
}
}
public class employee : cls
{
public new void display()
{
Console.WriteLine("welcome");
}
}
Description:- Here I have maked the object of the employee class, both display method of parent class and child class will get in memory. since the reference variable is cls class, so cls class display method will be called and print "hello" ,but if the reference variable will be of the employee class,then employee class display method will be called and print "welcome".There are some steps to execute the method hiding program .
Step1:- Open visual studio->click File->New Project->Select Console Application-> give your application name below(method hiding).
See it:-
Step2:- Copy the above code and paste program.cs File.
See it:-
Step3:- Run the program(press F5).
See it:-
I hope this is useful for you.
For More:-
click below for download whole application.
DOWNLOAD
shouldnt it print welcome instead of hello? I guess the screenshot is not correct
ReplyDelete