locked
How to invoke a method defined by a derived class from the base class reference? RRS feed

  • Question

  • Code Block

    public class Information

    {

        public virtual void ToLog();

    }

     

    public class InformationA : Information

    {

        public override void ToLog()

        {

    // Override details.

        }

    }

     

    public class InformationB : Information

    {

    public override void ToLog()

    {

    // Override details.

    }

    }

     

    // Somewhere else in the program

    ...

     

    // Once the information has been processed, I receive them and cast them into an

    // "Information" reference.

    Information i = ReceiveInformation();

     

    ...

     

    I created some sort of couple mechanism as above, I basically make any message(a.k.a information) which needs to be sent and processed derive from the "Information" base class. Then I send them to the code block where processes them and receive them by casting them into the base "Information" class reference.

     

    Here, I encountered this problem. Since I am not able to get the derived class reference at run time, how can I call the individual ToLog() while I am only holding the base "Information" class reference.

     

    Thanks in advance for any tips.

    Tuesday, November 6, 2007 9:57 AM

Answers

  • Yes, both the cases the Derived Class’s override method only called. Since it is virtual the derived class’s override method only called.

    If you want to call the Base class ToLog() function you have to use the base.ToLog() on the overridden ToLog() function.

    NOTE:

    The following 2 statements are identical (intiating base class object with Derived class & “..as BaseClassName”)

    Base baseclass = new DerivedA();

     

     

    DerivedA derivedclass = new DerivedA();

    baseclass = derivedclass as Base;

     

    Tuesday, November 6, 2007 12:12 PM
  •  YouMiss wrote:

    Yeah, sure.

     

    Different derived class has different implementation regarding to ToLog() function. As I stated, I just need a couple mechanism that I can transfer any class which derives from a base class and use the ToLog() differently according to the individual functionality of each derived class.

     

    You already have that mechanism. When you invoke the ToLog method, the correct implementation will be executed depending on what the actual object you are executing it on is.

     

    Base x = new SubClass();

    x.ToLog(); <--- SubClass's implementation of ToLog is executed, because x is actually referencing a SubClass object, not a Base object.

     

    This is just standard object-oriented polymorphism working as designed. I see nothing special about your question.

    Tuesday, November 6, 2007 4:04 PM

All replies

  •  

    Hi,

     

    If the method is defined in base class and is override in derived class.

     

    As you stated even if you use Information class reference and object is of type InformationA. Then when you call method at runtime it will call ToLog method of InformationA class. This is class late binding.

     

    If it doesn't answer your question just let me know.

     

    Regards,

    Manju sandhu

    Tuesday, November 6, 2007 10:18 AM
  • How about calling the base.ToLog() on the override methods?

    Code Block

        public class Information

        {

            public virtual void ToLog()

            {

                //Information details.

            }

        }

     

        public class InformationA : Information

        {

            public override void ToLog()

            {

                base.ToLog();

                // Override details.

            }

        }

     

        public class InformationB : Information

        {

            public override void ToLog()

            {

                base.ToLog();

                // Override details.

            }

        }

     

     

     

    Tuesday, November 6, 2007 11:41 AM
  • Thanks for all this...

     

    But, I guess I should say sorry. Indeed, I wanted to invoke the method: ToLog() which has been overriden by all the derived classes instead of calling the original virtual one defined inside the base class - "Information". Since then, I am going to extend the question a bit.

     

    I used to make use of an 'as' cast when I am implementing the class hierarchy, that's the reason I am confused.

     

    Code Block

    public class Base

    {

    public virtual void ToLog();

    }

     

    public class DerivedA : Base

    {

    public overide void ToLog();

    }

     

    public class DerivedB : Base

    {

    public overide void ToLog();

    }

     

    public class Program

    {

    public static Main()

    {

    Base baseclass = new DerivedA();

    baseclass.ToLog();

     

    DerivedB derivedclass = new DerivedB();

    baseclass = derivedclass as Base;

    baseclass.ToLog();

    }

    }

     

    Would you please explain me a bit about which function has been called in the two sceanarios above?

     

     

     

     

    Tuesday, November 6, 2007 11:57 AM
  • Hi,

     

    Do you have some different implementation of ToLog in derived class?

     

    Regards,

    Manju sandhu

     

    Tuesday, November 6, 2007 12:11 PM
  • Yes, both the cases the Derived Class’s override method only called. Since it is virtual the derived class’s override method only called.

    If you want to call the Base class ToLog() function you have to use the base.ToLog() on the overridden ToLog() function.

    NOTE:

    The following 2 statements are identical (intiating base class object with Derived class & “..as BaseClassName”)

    Base baseclass = new DerivedA();

     

     

    DerivedA derivedclass = new DerivedA();

    baseclass = derivedclass as Base;

     

    Tuesday, November 6, 2007 12:12 PM
  • Hi,

     

    I am agree with Manivannan.D.Sekaran. I want to add one more thing in his description that if you are not doing anything new in derived class ToLog then simply don't overide this method.

     

    Regards,

    Manju Sandhu


    Tuesday, November 6, 2007 12:16 PM
  • Yeah, sure.

     

    Different derived class has different implementation regarding to ToLog() function. As I stated, I just need a couple mechanism that I can transfer any class which derives from a base class and use the ToLog() differently according to the individual functionality of each derived class.

    Tuesday, November 6, 2007 2:22 PM
  •  YouMiss wrote:

    Yeah, sure.

     

    Different derived class has different implementation regarding to ToLog() function. As I stated, I just need a couple mechanism that I can transfer any class which derives from a base class and use the ToLog() differently according to the individual functionality of each derived class.

     

    You already have that mechanism. When you invoke the ToLog method, the correct implementation will be executed depending on what the actual object you are executing it on is.

     

    Base x = new SubClass();

    x.ToLog(); <--- SubClass's implementation of ToLog is executed, because x is actually referencing a SubClass object, not a Base object.

     

    This is just standard object-oriented polymorphism working as designed. I see nothing special about your question.

    Tuesday, November 6, 2007 4:04 PM