Results 1 to 5 of 5

Thread: Method overriding versus method hiding in C#

  1. #1
    Join Date
    Dec 2010
    Posts
    8

    Method overriding versus method hiding in C#

    Hello guys, I wanted to what is the difference between the method overriding and hiding method? I think that it is same but a friend of mine is saying that it is completely different. I am having my exams next week and I still don’t know, what is the difference between the overriding method and hiding method? Can anyone please let me know what the difference is? If any one having any knowledge or ant information then please let me know as soon as possible.

  2. #2
    Join Date
    Apr 2009
    Posts
    488

    Re: Method overriding versus method hiding in C#

    Overriding is possible by defining a method in the subclass that has the same name, same arguments and same return type as a method in the superclass. In C# overriding is done, provided that:
    1) We specify the method in base class as virtual.
    2) Implement the method in subclass using the keyword override.
    This method is completely different from Hiding method many a times many of them get confused between method overriding and hiding method. But the fact is that both are different terms and have different meaning.

  3. #3
    Join Date
    May 2009
    Posts
    529

    Re: Method overriding versus method hiding in C#

    using System:
    class Super;
    {
    protected int x;
    public Super (int x)
    { this.x = x; }
    public virtual void Display()
    { Console.WriteLine(“Super x = “ + x); }
    }
    class Sub : Super
    {
    int y;
    public Sub (int x, int y) : base (x)
    { this.y = y; }
    public override void Display()
    { Console.WriteLine(“Super x = “ + x);
    Console.WriteLine(“Sub y = “ + y); }
    }
    class OverrideTest
    {
    public static void Main()
    {
    Sub S1 = new Sub (100,200);
    S1.Display();
    }
    }
    The above is the example of Method overriding in C#. This is how it is been functioned in C sharp.

  4. #4
    Join Date
    May 2009
    Posts
    637

    Re: Method overriding versus method hiding in C#

    The keyword new is used to tell the compiler that the derived class method “hides” the base class method. If the new modifier is used in a declaration of a member that does not hide any inherited member, then the compiler will issue a warning. It is an error to use both the new and override modifiers in the same declaration. This is an important aspect of C sharp where in it functions accordingly.

  5. #5
    Join Date
    May 2009
    Posts
    539

    Re: Method overriding versus method hiding in C#

    using System;
    class Base
    {
    public void Display()
    { Console.WriteLine(“Base Method”); }
    }
    class Derived : Base
    {
    public new void Display()
    { Console.WriteLine(“Derived Method”); }
    }
    class HideTest
    {
    public static void Main()
    { Derived d = new Derived();
    d.Display(); }
    }
    The output for the above program will be “Derived Method”. This is an example of hiding method which acts completely different from overriding method.

Similar Threads

  1. Which method should I use to mod PS2
    By Jalela in forum Portable Devices
    Replies: 6
    Last Post: 03-07-2011, 10:30 AM
  2. How to use PUT method in PHP?
    By Emma.J in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 05:06 AM
  3. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:36 AM
  4. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  5. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,718,329.54677 seconds with 17 queries