1
votes

Can someone help me understand

  1. Is this statement correct: BaseClass bcdc = new DerivedClass(); which means bcdc is of type BaseClass class, and its value is of type DerivedClass object? Also, what does that mean and why would an object be instantiated like that as opposed to having the class type be the same as the new object being instantiated, as in DerivedClass dc = new DerivedClass()?
  2. Why bcdc.Method1() => Derived-Method1. Is it because the keyword override is used, and is so overriding the virtual Method1?
  3. Why bcdc.Method2() => Base-Method2. I am confused because the new key word in the DerivedClass should be hiding Base-Method2? I thought that was the functionality of the new keyword.
class BaseClass  
{  
    public virtual void Method1()  
    {  
        Console.WriteLine("Base - Method1");  
    } 
    public void Method2()  
{  
    Console.WriteLine("Base - Method2");  
} 
}  
  
class DerivedClass : BaseClass  
{  
    public override void Method1()  
{  
    Console.WriteLine("Derived - Method1");  
}  
    public new void Method2()  
    {  
        Console.WriteLine("Derived - Method2");  
    }  
}
class Program  
{  
    static void Main(string[] args)  
    {  
        BaseClass bc = new BaseClass();  //bc is of type BaseClass, and its value is of type BaseClass
        DerivedClass dc = new DerivedClass();  //dc is of type DerivedClass, and its value is of type DerivedClass
        BaseClass bcdc = new DerivedClass();  //bcdc is of type BaseClass, and its value is of type DerivedClass.
                     
        bc.Method1();  //Base - Method1 
        bc.Method2();  //Base - Method2
        dc.Method1();  //Derived - Method1
        dc.Method2();  //Derived - Method2 
        bcdc.Method1(); //Derived - Method1. ??
        bcdc.Method2(); //Base - Method2.  ??
    }   
} ```
1
Consider a drawing app. It has a base DrawObject & several derived classes: Rectangle, Circle & Line. The screen is managed in a List<DrawObject> drawList. When a user creates a new circle on the screen, the code does something like drawList.Add(new Circle(params)); which is effectively what you are seeing (creating a new instance of a base class and assigning it to a reference typed to being the base class. In the DrawObject class, there's a method marked virtual called Draw. Each subclass overrides that method. Drawing everything is simply looping and calling DrawFlydog57
docs.microsoft.com/en-us/dotnet/csharp/language-reference/… - It uses the same example I did. Also consider an app that manages a List<Animal> pets collection. Subclasses could include Dog, Cat and Bird. Each subclass overrides the virtual method Speak. Dogs speak "woof", cats, "meow" and birds "chirp". That's an example that's easy to create and test in a simple console app.Flydog57
@Flydog57 Thank you for the link and explanations. I got my example from the Microsoft docs.docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… I understand overriding and the use of virtual, but am not understanding why bcdc.Method2(); is outputting Base-Meth DerivedClass Method2() has the new keyword. I would expect the out put to be Dervied-Method2. Can you point me in the right direction to understand this behavior?SvetlanaR
When you add the new keyword to a method like that, you are saying "yes, I understand that my base class has a method named Method2, but I want to have a method named Method2 that has a different meaning. Consider a CowboyArtist class that inherits from Cowboy. The Cowboy class has a method named Draw which means Draw your gun from your holster. The CowboyArtist class wants Draw to mean Make a drawing, so it tags its implementation with new. The two methods have the same name, but different semantics and are not related at all.Flydog57

1 Answers

0
votes

which means bcdc is of type BaseClass class, and its value is of type DerivedClass object?

Yes. I would however prefer to call it a DerivedClass object with a BaseClass reference

Also, what does that mean and why would an object be instantiated like that as opposed to having the class type be the same as the new object being instantiated, as in DerivedClass dc = new DerivedClass()?

One case is when you want to call an explicit interface method. However, a very similar and more common case would happen if you call a method: MyMethod(BaseClass bcdc).

In this simple program all types are easily known at compile time. But for larger programs this would not be the case, a method take a BaseClass parameter that can have a bunch of different implementations, and all the implementations might not be known when the code is compiled.

Why bcdc.Method1() => Derived-Method1. Is it because the keyword override is used, and is so overriding the virtual Method1?

Yes, for methods marked as virtual the compiler will insert a check that forwards the method call to the overridden method based on the actual object type, i.e. DerivedClass. This is sometimes called virtual dispatch or dynamic dispatch. I.e. the method will be selected dynamically at runtime based on the object type.

Why bcdc.Method2() => Base-Method2. I am confused because the new key word in the DerivedClass should be hiding Base-Method2? I thought that was the functionality of the new keyword.

Method2 is not virtual, so there will be no dynamic dispatch. So the reference type, i.e. BaseClass, will be used to determine the called method. This is called static dispatch, i.e. the called method can be determined statically at compile time.