Can someone help me understand
- 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 inDerivedClass dc = new DerivedClass()
? - Why
bcdc.Method1() => Derived-Method1
. Is it because the keyword override is used, and is so overriding the virtual Method1? - 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. ??
}
} ```
DrawObject
& several derived classes:Rectangle
,Circle
&Line
. The screen is managed in aList<DrawObject> drawList
. When a user creates a new circle on the screen, the code does something likedrawList.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 theDrawObject
class, there's a method markedvirtual
calledDraw
. Each subclass overrides that method. Drawing everything is simply looping and callingDraw
– Flydog57List<Animal> pets
collection. Subclasses could includeDog
,Cat
andBird
. Each subclass overrides the virtual methodSpeak
. Dogs speak "woof", cats, "meow" and birds "chirp". That's an example that's easy to create and test in a simple console app. – Flydog57bcdc.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? – SvetlanaRnew
keyword to a method like that, you are saying "yes, I understand that my base class has a method namedMethod2
, but I want to have a method namedMethod2
that has a different meaning. Consider aCowboyArtist
class that inherits fromCowboy
. The Cowboy class has a method namedDraw
which means Draw your gun from your holster. The CowboyArtist class wantsDraw
to mean Make a drawing, so it tags its implementation withnew
. The two methods have the same name, but different semantics and are not related at all. – Flydog57