-1

The book on "C# 3.0- The Complete Reference" (p.295)says:

The program creates a base class called Base and two derived classes, called Derived1 andDerived2. Base declares a method called Who( ), and the derived classes override it. Inside the Main( )method, objects of type Base, Derived1, and Derived2 are declared. Also, a reference of type Base, called baseRef, is declared. The program then assigns a reference to each type of object to baseRef and uses that reference to call Who( ). As the output shows, the version of Who( )executed is determined by the type of object being referred to at the time of the call, not by the class type of baseRef

Now the point is that ,is it necessary that we should use a basereference and assign to it the object of each derived class in order to take advantage of method overriding and virtual keyword as i already tested that it worked correctly too ,instead when i didn't use any basereference.

Here you go:

class Base
{
    // Create virtual method in the base class.  
    public virtual void Who()
    {
        Console.WriteLine("Who() in Base");
    }
}
class Derived1 : Base
{
    // Override Who() in a derived class. 
    public override void Who()
    {
        Console.WriteLine("Who() in Derived1");
    }
}

class Derived2 : Base
{
    // Override Who() again in another derived class. 
    public override void Who()
    {
        Console.WriteLine("Who() in Derived2");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Base baseOb = new Base();
        Derived1 dOb1 = new Derived1();
        Derived2 dOb2 = new Derived2();

        Base baseRef; // a base-class reference 

        baseRef = baseOb;
        baseRef.Who();

        baseRef = dOb1;
        baseRef.Who();

        baseRef = dOb2;
        baseRef.Who();

        //but this works too the same

          baseOb.Who();
          dob1.Who();
          dob2.Who();



    }
}

}

Be brave enough to answer it rather than downvote !!!

  • 1
    I like the *As the output shows* bit. –  Oct 08 '14 at 09:42
  • 1
    You might be better off asking this question with a code sample. But I think the basereference is being used to show that even though the compiler is just told it is of type `Base` that it is still calling the method from `Derived1`, not the `Base` method. – Chris Oct 08 '14 at 09:42
  • So , go after your like now :-P @MrCoder – Javed Iqbal Oct 08 '14 at 09:59
  • You can have a better picture now @Chris – Javed Iqbal Oct 08 '14 at 09:59
  • I can't approach you via profile(once you offered ) but please have a look here: http://stackoverflow.com/questions/26190895/how-to-get-the-type-of-data-from-a-url/26191006?noredirect=1#comment41081130_26191006 @MrCoder – Javed Iqbal Oct 08 '14 at 10:01
  • @JavedIqbal I honestly don't know-have looked at some of the answers and am still none the wiser. All I can say is there must be a way of getting the metadata/querying for it before dl attempt :( sorry –  Oct 08 '14 at 10:07

1 Answers1

0

The point that they are trying to make here is to clarify that if you do Base baseRef = new Derived1() that the behaviour used when you are overriding virtual methods is that of the derived class. IF you didn't know how it should work you might think that if your reference is of type Base that it will use the method on that class. This is of course wrong and you know that but it is nice to demonstrate it to people.

That behaviour does exist if you use the new keyword instead of override. Try changing your class to this and run it again:

class Derived2 : Base
{
    // Override Who() again in another derived class. 
    public new void Who()
    {
        Console.WriteLine("Who() in Derived2");
    }
}

Now you will see that your final block works exactly as before but that when BaseRef is dOb2 that it actually calls the method on the base class. This is because override replaces the virtual method call whereas new says that you have a different method in the derived class and the way to call the one on the base class is like that. http://msdn.microsoft.com/en-us/library/435f1dw2.aspx talks about this in detail.

It should be noted that you very very rarely want to use the new modifier so don't dwell on this too long.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • @JavedIqbal: To be fair the original question wasn't a very good question and sadly the people who downvoted are unlikely to come back and see the changes you've made. Sometimes people have some time to constructively critique a question but nobody has time to do that everywhere so sometimes people will downvote and move on. – Chris Oct 08 '14 at 10:26
  • But why they downvote if they are unwilling to edit/suggest ...They may 'pass' and let the others like you and @MrCoder to comment :( – Javed Iqbal Oct 08 '14 at 10:30
  • They just discourage OP :( – Javed Iqbal Oct 08 '14 at 10:31
  • Some people don't respond well to feedback (in that they keep asking rubbish questions and don't make the improvements suggested). You sadly got lumped into that category by more jaded people than I. :) – Chris Oct 08 '14 at 10:37