Calling Derived class method from base class reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vishalja1981
    New Member
    • Mar 2010
    • 2

    Calling Derived class method from base class reference

    ------------------------------------------------------------------------------------------------------------------
    Please see simplified version of below program. First part of code declare several classes. Class A is simple class have one member method1. Class B and C both derived from A implement same member method1.
    ------------------------------------------------------------------------------------------------------------------

    I have arrayA of type A in main, this store instance of B and C. See main in code where I want to call method1 of B and C how I call that.
    1. Class A
    2. {
    3. method1()
    4. {
    5. console.write(" XYZ");
    6. }
    7. }
    8. Class B:A
    9. {
    10. new method1()
    11. {
    12. // some statement
    13. }
    14. }
    15. Class C:A
    16. {
    17. new method1()
    18. {
    19. // some code
    20. }
    21. }
    22. main()
    23. {
    24. arrayA.add(objB );
    25. arrayA.add(objC );
    26. foreach(A a in arrayA)
    27. {
    28. a.method1();
    29. }
    30. }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You can access the base class in both B and C. Perhaps something like this...?

    Code:
    ...
    public class B : A
    {
      new method1() { ... }
      public CallBaseMethod1() { base.method1(); }
    }
    
    ...
    
    arrayA.add(objB);
    arrayA.add(objC);
    foreach(B b in arrayA)
    {
      b.CallBaseMethod1();
    }
    Alternatively, in A you could make a method that does the work of A but call it from another method, then all your classes that inherit A will have two methods, one that's overridden and another that's not.

    I don't know if there's a way to just force a call to the base method though. There might be, but I don't know what it is. Sorry!

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      In your current context, A::Method1() would be called each time.
      If you're trying to do what I think you're trying to do:
      Declare A's method virtual.
      Declare B and C's method as a override function.

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        I think, you have to cast it to the correct type.
        Code:
        foreach(A a in arrayA)
        {
          if(a is B)
            ((B)a).method1();
          elseif(a is C)
            ((C)a).method1();
          else
            a.method1();
        }
        If you don't need to instantiate objects of type A (A a = new A();),
        you could make A's method1 abstract.

        Code:
        abstract class A {
          abstract void method1();
        }
        
        class B : A {
          override void method1() {
            // some code
          }
        }
        another way is to use delegates

        Code:
        private class B : A {
              void method1ofB() {
                // doing stuff of class B
              }
              
              public B() {
                Method1 = method1ofB;
              }
            }
        
            private class A {
              public Action Method1;
        
              private void method1ofA() {
                // some stuff of class A
              }
        
              public A() {
                Method1 = method1ofA;
              }
            }
        
        
        // you'd call it this way:
        A a1 = new A();
        A a2 = new B();
        a1.Method1();  // doing A's stuff
        a2.Method1();  // doing B's stuff

        Comment

        • vishalja1981
          New Member
          • Mar 2010
          • 2

          #5
          Thaks Guys. I got it. All of above works and I used making base class method virtual and derived override. It solved my needs.

          Comment

          Working...