locked
When to use a base class reference to create an object of the derived type? RRS feed

  • Question

  • BaseClass B = new DerivedClass()

    I have seen this kind of 'simple assignment and implicit cast/conversion' used a lot of times. Usually in an OOPs questionaire :P. Personally I have never felt the need to do so.

    When exactly is this useful?

    • Moved by litdev Monday, September 17, 2012 11:48 AM (From:Small Basic)
    Monday, September 17, 2012 7:24 AM

Answers

  • Hi,

    first of all: This forum is covering the product Small Basic only. I would suggest to ask your question in a more approiate forum (e.g. the Visual C# General forum in case you use C#).

    In general, you use that whenever you want a reference to a BaseClass instead of a reference to a DerivedClass. That is often not requiredm, because it mostly does not make a real difference. (The implicit cast will be done whenever you use it later if required).

    One difference could occur when you have a method that is overridden. If the overridden method wasn't virtual, then the method called is differently.

    class A
    {
      public virtual void M1() { }
      public void M2() {}
    }

    class B : A
    {
      public override void M1() { }
      public new void M2() {}
    }

    A a = new B();
    B b = new B();
    a.M1(); // B.M1() is called
    a.M2(); // A.M1() is called because method wasn't virtual
    b.M1(); // B.M1() is called
    b.M2(); // B.M2() is called

    With kind regards,

    Konrad

    • Marked as answer by lohiarahul Monday, September 17, 2012 10:21 AM
    Monday, September 17, 2012 9:13 AM

All replies

  • Is this a question about the Small Basic language?

    Jan [ WhTurner ] The Netherlands

    Monday, September 17, 2012 9:12 AM
  • Hi,

    first of all: This forum is covering the product Small Basic only. I would suggest to ask your question in a more approiate forum (e.g. the Visual C# General forum in case you use C#).

    In general, you use that whenever you want a reference to a BaseClass instead of a reference to a DerivedClass. That is often not requiredm, because it mostly does not make a real difference. (The implicit cast will be done whenever you use it later if required).

    One difference could occur when you have a method that is overridden. If the overridden method wasn't virtual, then the method called is differently.

    class A
    {
      public virtual void M1() { }
      public void M2() {}
    }

    class B : A
    {
      public override void M1() { }
      public new void M2() {}
    }

    A a = new B();
    B b = new B();
    a.M1(); // B.M1() is called
    a.M2(); // A.M1() is called because method wasn't virtual
    b.M1(); // B.M1() is called
    b.M2(); // B.M2() is called

    With kind regards,

    Konrad

    • Marked as answer by lohiarahul Monday, September 17, 2012 10:21 AM
    Monday, September 17, 2012 9:13 AM
  • Thank you
    Monday, September 17, 2012 10:22 AM
  • Is this a question about the Small Basic language?

    Jan [ WhTurner ] The Netherlands

    This Forum is for SmallBasic Programming, NOT 'Small or Basic questions'

    Read this announcement on the forum now. oops.


    Rahul Lohia. Software Developer. Microsoft Technologies.

    Monday, September 17, 2012 10:32 AM