?

Log in

Previous Entry | Next Entry

This question is taken from .NET interview questions with answer book published by Bpb publications.

Well two differences

  1. Shadowing completely replaces the base class member whereas Overriding just overrides the implementation.
    Example we can have something like this,
    public class Base
    {
    publicintMyMember()
    {
       //Do something
      return 5;
    }
    }
    public class Derived:
    {
    public new float MyMember()
    {
       //Do something
      return 5.5;
    }
    }
    Overriding just allow you to redefine implementation (won’t replace).
    public class Base
    {
    Public virtual intMyMember()
    {
        //Do something
       return 5;
    }
    }
    //Following will throw error
    public class Derived:
    {
    public override float MyMember()
    {
         //Do something
        return 6.6;
    }
    }

    //Following will work
    public class Derived:
    {
    public override intMyMember()
    {
          //Do something
         return 6;
    }
    }

  2. In Shadowing - Base class object always invoke base class member always.
    Base b=new Base();
    b.MyMember();è5
    Base b=new Derived();
    b.MyMember();è5
    Derived b=new Derived ();
    b.MyMember();è5.5

    In Overriding it depends on with what we are initializing.
    Base b=new Base();
    b.MyMember();è5
    Base b=new Derived();// Base class type initialized to derived class object
    b.MyMember();è6
    Derived b=new Derived ();
    b.MyMember();è6

    Do watch the below video on C# Shadowing interview questions

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

January 2017
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Tags

Powered by LiveJournal.com