Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You haven't performed an implicit conversion as you haven't actually converted anything.</p> <pre><code>A a = new B(); </code></pre> <p>You have directly instanciated an object of type B. You can reference it from a variable of type A because B is a sub class of A. However, the two actions should be considered as independant.</p> <p>... = new B();</p> <p>will always instanciate an object of type B, regardless of what is on the left.</p> <pre><code>A a = ... </code></pre> <p>Will always try to assign what is on the right to the variable a.</p> <p>The only difference that the type of the reference will make is in how the object is seen by the CLR.</p> <pre><code>A a = new B(); B b = new B(); </code></pre> <p>Both are objects of type B. However, 'a' will only allow you to work with it as if it were an object of type A. So, you can only call a.show() and a.test(), even although it is actually of type B and so does have the additional method testb(). 'b' will allow you to call b.show(), b.test() and b.testb(). Regardless of the variable type, when you call show(), it is of type b and so it is the overriden show() method that is returned.</p> <p>Finally, what you can now do is downcast. i.e. you instanciated an object of type B so you can now cast it to a variable of type b and so allow full access to all of type B members.</p> <p>e.g.:</p> <pre><code>A a = new B(); a.testb(); // this will not compile as a does not have a definition of testb(). A a = new B(); B b = (B)a; // downcast a to a reference of type B b.testb(); // this is now fine </code></pre> <p>I just saw you additional question of why would you want to instanciate something as type A, i.e. the base class. This is to provide a common ground as a way of working with objects of different types. For example, you may have a base type of Animal and derrive from it several sub classes - Cat, Dog, Gerbil. However, you may then want to be able to pass a list of all Animals to a method that only needs to work with common properties. So, if Animal had a name, your List could be iterated and each Animals name referred to, even although Animal could itself be Abstract (Not able to be instanciated) and the list comprise entirely of Dogs, Cats and Gerbils. You can also then downcast the various animals by finding out what there type is to interact with them further, e.g.</p> <pre><code>if (animal is Dog) { Dog dog = (Dog)animal; } </code></pre> <p>Or you could use the 'as' keyword to do the cast:</p> <pre><code>Dog dog = animal as Dog; </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload