locked
base class methods calling derived class methods ?

    Question

  • Ive inherited a C# class library and it seems that several of the base classes implement some methods that call methods which are defined in classes that are derived from the calling method's class. Is this "normal" ?

    eg:
    BaseClass
    method1
    method2 {
    //some code here
    some_int = get_the_int();
    }



    DerivedClass:BaseClass
    method1
    method2
    get_the_int () {

    //get the int here...
    }


    Wednesday, August 02, 2006 8:34 PM

All replies

  •  

    Hi,

     How the base class can call dervied calss method with out instance.

    Inheriance main concept is derived class can use the base class proeprties and behaviour, but viseversa is not possible.

     

    BaseClass
    method1
    method2 {
    //some code here
    some_int = DerivedcalssObj.get_the_int(); // only this is possible.
    }

     

    hope now you clear.

     

    -Thanks

    Mahesh

    Thursday, August 03, 2006 10:28 AM
  • If you have a method marked as virtual in the base class and overriden in sub classes, than, everytime that base classe's methods (constructor too) invoke the virtual method the actual method executed is the most overriden. This is a common issue in polymorphism semantics actually addressed in many Guru Forums, in the sense: it's coherent with the definition of polimorphism but "sometimes" it's a bit dangerous, it should be nice to have a third way between a virtual method and a not one.

    Here an example:

    public class DerivedClassExampleSuper
    {
       public DerivedClassExampleSuper()
       {
          WriteSomethingOut();
       }

       public virtual void WriteSomethingOut()
       {
         
    Console.WriteLine("SuperClass");
       }
    }

    public class DerivedClassExampleSub : DerivedClassExampleSuper
    {
      
    public override void WriteSomethingOut()
       {
         
    Console.WriteLine("SubClass");
      
    }
    }

    if you simply do this:

    DerivedClassExampleSub subClass = new DerivedClassExampleSub();

    you'll see prompted "SubClass".

    Thursday, August 03, 2006 1:44 PM
  • "Inheriance main concept is derived class can use the base class proeprties and behaviour, but viseversa is not possible."

    If you're talking about polymorphic inheritence, then this is completely wrong.  It is a common design pattern for base classes to call methods which are defined by derived classes, which is accomplished through either virtual or abstract methods.  The System.Collections.CollectionBase class is a good example of this pattern.

    Thursday, August 03, 2006 6:01 PM
  • To build on Nimrand's point, here's an oo example:

    Suppose you have a base class for a working professional and you derive from it.  You know each professional is going to do some work, so you define a DoWork() method in the base class, but you don't know exactly how that work is going to be performed.  And in this case, there is no default behavior for DoWork() so you mark it as abstract to force the derived classes to implement it.  Then you have a method to please your boss, which in turn calls your DoWork method.  Your classes would look something like:

    public abstract class Professional
    {
         public abstract void DoWork();
         public void PleaseBoss()
         {
              DoWork();
         }
    }

    public class Doctor : Professional
    {
         public override void DoWork()
         {
               // Operate on patient
         }
    }

    public class Programmer : Professional
    {
         public override void DoWork()
         {
              // Write bugs
         }
    }

    Jayson

    Thursday, August 03, 2006 9:04 PM
  • Understood. how do you tweak this when each type of professional has some common work to do each time they do their own specialist work. e.g register a diary log or something

    Monday, August 10, 2009 1:22 PM
  • public class Programmer : Professional
    {
         public override void DoWork()
         {
              // Write bugs
         }
    }

    public class TimeProgrammer : Programmer
    {
         public override void DoWork()
         {
              WriteStartTime();
              base.DoWork();
              WriteEndTime();
         }
    }
    Monday, August 10, 2009 4:02 PM