Results 1 to 4 of 4

Thread: Confused between override and new in C#

  1. #1
    Join Date
    Sep 2009
    Posts
    131

    Confused between override and new in C#

    I have started to learn C# due to my academic requirements. I am not at all good at software programming and C# is not an exception for me. I am not able to grasp many of the concepts in C# and even small topics are difficult to grasp for me. I am confused on the difference between override and new keywords in C#. What is their usefulness and differences ?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Confused between override and new in C#

    It is very simple. Using the override keyword the programmer can call the derived class method by overriding the base class method. But in case the programmer wants to hide base class method not override it by the derived class method then the programmer can use the keyword new. This is the most basic difference between keyword- override and keyword- new with C-Sharp.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Confused between override and new in C#

    Even I was totally confused when I had to study C sharp. Even I am not at all good at programming. But C sharp is not that difficult to understand if you are willing to. The override keyword is used more often with functions. This is because in the derived class the override keyword allows the user to have different definitions to Base class function. Where as the when the user wants to assign memory to a variable the new keyword is useful.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Confused between override and new in C#

    Here is an example of the new keyword in C Sharp:
    Code:
    public class Parent
    {
        public virtual void temp()
        {
         .
         .
         .
        }
    }
    
    public class Child : Parent
    {
        public new void temp()
        {
         .
         .
         .
        }
    }
    .
    .
    .
    
    Parent p = new Child();
    Child c  = new Child();
    p.temp();
    c.temp();

    This is the program for override keyword in C Sharp:
    Code:
    public class Parent
    {
        public virtual void temp()
        {
         .
         .
         .
        }
    }
    
    public class child : Parent
    {
        public override void temp()
        {
         .
         .
         .
        }
    }
    .
    .
    .
    Parent p = new child();
    p.temp();
    This topic is all related to the polymorphism concept of C Sharp. In a scenario where the programmer does not provides the override or new keywords the throughput is as if the programmer made use of the new keyword. But the compiler us expected to generate a compile time error.

Similar Threads

  1. How to override ToString() method in C#?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 07:54 PM
  2. How to use Override annotation in java?
    By MAHESA in forum Software Development
    Replies: 4
    Last Post: 30-01-2010, 08:25 PM
  3. How do I override my CSS link colors?
    By Bhadrak in forum Software Development
    Replies: 4
    Last Post: 10-08-2009, 05:23 AM
  4. Administrator Password Override
    By Saaz in forum Operating Systems
    Replies: 3
    Last Post: 01-07-2009, 11:25 AM
  5. How to override vista permissions
    By Reckon in forum Operating Systems
    Replies: 3
    Last Post: 01-06-2009, 11:16 AM

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,265.74550 seconds with 16 queries