Definition of Method Overriding for Beginners

Lakhangarg
Posted by in C# category on for Intermediate level | Views : 6208 red flag

Definition of Overrtide for Beginners with the help of a example.
Introduction
If a new developer add one new method in child class but unfortunately that same method present in parent class also. These situations are called method hiding.
Sample:
class Home
    {
        public void Fan()
        {
            Console.WriteLine("Home--->Fan");
        }
    }
    
    class Hotel:Home
    {
        public void Fan()
        {
            Console.WriteLine("Hotel--->Fan");
        }
    }

    class Building
    {
        static void Main(string[] args)
        {
            Home objhome = new Home();
            Hotel objhotel = new Hotel();
            objhome.Fan();
            objhotel.Fan();
        }
    }

In the above sample class home as well as class hotel having the same method called fan(). Then in main, I created separate objects for those 

classes and called their individual methods. yup its working fine but its working with one warning what’s that.

Result:

Home---->Fan
Hotel--->Fan

Warning :
‘MethodHiding.Hotel.Fan ()' hides inherited member ‘MethodHiding.Home.Fan ()'. Use the new keyword if hiding was intended.

By seeing the warning itself we identify the hotel class fan method hiding the home class fan method. What to-do for removing the warning? In 

csharp we achieve very easily see the following code

To remove the warning

class Home
    {
        public void Fan()
        {
            Console.WriteLine("Home--->Fan");
        }
    }
    class Hotel:Home
    {
      new public void Fan()
        {
            Console.WriteLine("Hotel--->Fan");
        }
    }

    class Building
    {
        static void Main(string[] args)
        {
            Home objhome = new Home();
            Hotel objhotel = new Hotel();
            objhome.Fan();
            objhotel.Fan();
        }
    }

Now you have to see the situation like this also just go through the program for a minute
See in this case:

class Home
    {
        public void Fan()
        {
            Console.WriteLine("Home--->Fan");
        }
    }

    class Hotel:Home
    {
      new public void Fan()
        {
            Console.WriteLine("Hotel--->Fan");
        }
    }

    class Building
    {
        static void Main(string[] args)
        {
            Home objhome = new Home();
            Hotel objhotel = new Hotel();
            objhome = objhotel;
            objhome.Fan();
            Console.Read();
        }
    }

Output:
Home--->Fan 

Finally the better solution is: 

class Home
    {
        public virtual void Fan()
        {
            Console.WriteLine("Home--->Fan");
        }
    }
    class Hotel:Home
    {
       public override void Fan()
        {
            Console.WriteLine("Hotel--->Fan");
        }
    }

    class Building
    {
        static void Main(string[] args)
        {
            Home objhome = new Home();
            Hotel objhotel = new Hotel();
            objhome = objhotel;
            objhome.Fan();
            Console.Read();
        }
    }

Hotel--->Fan 
Page copy protected against web site content infringement by Copyscape

About the Author

Lakhangarg
Full Name: Lakhan Pal
Member Level: Silver
Member Status: Member,Moderator
Member Since: 8/17/2009 12:39:46 AM
Country: India

http://lakhangarg.blogspot.com
Hello Friends Myself Lakhan Pal Garg and i am a B.Tech (IT) Graduate and having 8+Years of Exp. in Microsoft Technology. I have Write a Blog Named Free Code Snippets (http://lakhangarg.blogspot.com/) I hope you must visit my blog as your valuable feedback will motivate me to write more and improve my mistake. If you want to gain more knowledge then share it with others.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)