0

I don't know how to phrase this question properly, but I can't seem to find a similar question on StackOverflow

Say I have a abstract base class A and a child class B.

Apparently I can create the child object via both of these lines

A child = new B();

B child = new B();

And it seems like they support the same usage of methods.

What's the difference?

ygongdev
  • 264
  • 3
  • 19
  • A `B` is neccessarily an `A`. It has everything `A` has, and possibly more. If you added extra methods or properties in `B` that are not defined in `A`, you would not be able to use them if you treat your variable as an `A`. You would have to cast it back to a `B`. – Glorin Oakenfoot Jul 08 '16 at 20:50
  • @GlorinOakenfoot, ahhh make sense. I think the reason this came up was because I currently don't have any extra public methods in my child class, so A and B are technically supporting the same set of methods :) – ygongdev Jul 08 '16 at 20:57
  • @ygongdev Thats pretty typical. What usually changes between child classes is the *implementation* not the *interface* – BradleyDotNET Jul 08 '16 at 22:51

3 Answers3

0

Since B derives from A, B is A, thus you can set an instance of B to a reference typed as A.

That is, a B instance set to a reference of type A will be able to access members of A but not from B. This is an upcast (casting a reference from more to less typed).

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

In the first case the B instance is automatically down casted (as described on page 136 of the ECMA standard) to be of reference type A. This means available methods to call will be those found in A. Note that these might be fewer than those found in B. However, all abstract methods needs to be implemented by B.

For example, let B be defined like to

class abstract A
{
    public abstract void Foo();
}

class B
{
    public override void Foo() {}
    public void Bar() {}
}

Here the method Bar() will only be available in your second case - var B = new B ();

Note that if you use the var keyword, an expression e.g. new B() will be of type B.

vidstige
  • 12,492
  • 9
  • 66
  • 110
  • This is exactly where my curiosity was sparked. Apparently if I do `var A = new B()`; A can also access `Bar()` Which I guess kinda of made sense to me – ygongdev Jul 08 '16 at 20:54
  • Yes, very attentive! This is because the `var` keyword will interfere the type from the right hand expression, namely `new B()`. The type of this expression is `B` and hence, the variable `A` will be of type `B` despite its name. – vidstige Jul 08 '16 at 20:57
  • However, if you do `A a = new B();` you will find that the `Bar()` method is no longer accessible. – vidstige Jul 08 '16 at 20:57
  • Oh, but actually if I did A = new B() (without var), I can still access Bar() – ygongdev Jul 08 '16 at 21:00
  • Yes, if A is of type `B` you can – vidstige Jul 08 '16 at 21:02
  • Sorry, got confused. I've updated my example. Now the `Bar` method is in `B`. This should be a more helpful example. – vidstige Jul 08 '16 at 21:03
0

B can do everything A can do, but B can do more - whatever was defined in the child class is available to B (but not to A).

By assigning a B to an A variable, you (temporarily) remove this, and now it can do only what an A can do (until you assign it back to a B variable).

Aganju
  • 6,295
  • 1
  • 12
  • 23