Reputation: 2328
I've been studying class inheritance recently and I keep coming across this specific piece of code.
public class Foo {
public bool DoSomething()
{
return false;
}
}
public class Bar : Foo {
public new bool DoSomething()
{
return true;
}
}
public cass Test {
public static void Main () {
Foo test = new Bar ();
Console.WriteLine (test.DoSomething ());
}
}
What confuses me here is that, if it were me I would create an instance of Bar by type Bar...
Bar test = new Bar();
I don't understand why it would be created the way it is in the code.
Upvotes: 1
Views: 669
Reputation: 50215
If you consider that it only wants the functionality defined by Foo
that Bar
implements, it might make more sense. Bar
is free to add more functionality related to whatever Bar
is.
public class Bar : Foo {
public new bool DoSomething()
{
return true;
}
public bool IsValid {get;}
}
Now the calling code only cares about the abstraction defined by Foo
so, despite the fact you are creating Bar
, you only get the contract defined by Foo
(just DoSomething
) and therefore not Bar.IsValid
functionality I just defined.
Upvotes: 0
Reputation: 33242
You can assign a more generic (Foo) with a less generic type ( Boo ) this is usually done when you leverage the polymorphism. The classical example is having Animal, Dod and Cat and then you can say Animal=new Dog()
or Animal=new Cat()
, then you can call virtual function on it and having the proper function automatically called. This Is not your case since you are overlapping the function DoSomething with new ...
Upvotes: 0
Reputation: 160852
This code was probably written to demonstrate the difference between overriding and hiding a base class method:
In this case instantiating a Bar
object using a Foo
variable will use the base class method DoSomething()
and print out false
. Had the DoSomething
method been declared as virtual in the base class and been overridden in the derived class the output would be true
as in the following example:
public class Foo {
public virtual bool DoSomething()
{
return false;
}
}
public class Bar : Foo {
public override bool DoSomething()
{
return true;
}
}
Upvotes: 3
Reputation: 245389
It's easier to think of these things in real world objects.
Try thinking about a car. You can have different makes/models of car but each car performs the same base functionality.
Your code can work with objects in the same way. You write the code to work with any car, but you can really specify any make/model of car you want:
public class Car
{
public virtual void Drive()
{
}
}
public class ChevyVolt : Car
{
public override void Drive()
{
// Initialize the battery and go
}
}
public class CadillacEscalade : Car
{
public override void Drive()
{
// Check to ensure you have an oil field worth of gas and go
}
}
Now, with those definitions...you could create a class responsible for driving the car. It wouldn't matter what car. You just have to worry about driving:
public class Commuter
{
public void GoToWork()
{
// Garage.PickCar() could return either ChevyVolt or an Escalade
Car todaysRide = Garage.PickCar();
// Now that we have a car, go to work
todaysRide.Drive();
}
}
Upvotes: 1
Reputation: 38825
Bar
derives from Foo
, and so it is perfectly valid to create an instance of Bar
and assigned to a a Foo
-reference. I suspect the example code that you have shown is merely showing that Bar
is a Foo
and is therefore assignable.
Upvotes: 0
Reputation: 62027
Generally, it wouldn't be. A more realistic example would be
void DoSomethingWithAFoo(Foo f) {
f.DoSomething();
}
In this case, f
could be a Foo
or a Bar
, and DoSomethingWithAFoo
doesn't have to know or care.
Upvotes: 0