Author Topic: Theoretical OOP Question  (Read 564 times)

0 Members and 1 Guest are viewing this topic.

Bocsius

  • is calmer than you are
  • Senior Member
Theoretical OOP Question
« on: September 12, 2008, 08:05:14 PM »
So this afternoon I was revising a base class to centralize a task that was common among all derived classes while still giving the derived classes the ability to override part of the implementation. However, to make the base function work the way I wanted it to, I split it into two functions: an outer and an inner. The inner function is the part that the derived classes can still override.

So anyway, I'm still sort of noobish with some OOP principles due to my background not being heavy on such methodologies. To make a long story short... here's a simplified code sample:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;

namespace InheritanceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DerivedClass derivedClass = new DerivedClass();
            Console.Read();
        }
    }

    public class BaseClass
    {
        public void RunThis()
        {
            this.RunFromHere();
        }

        public virtual void RunFromHere()
        {
            Console.WriteLine("Base");
        }
    }

    public class DerivedClass : BaseClass
    {
        public DerivedClass()
        {
            this.RunThis();
        }

        public override void RunFromHere()
        {
            Console.WriteLine("Derived");
            base.RunFromHere();
        }
    }
}

Basically, I have a base class that has two functions to sort of display the outer/inner function I was working on before. The first base function calls the second function, except it has been overridden by the derived class. Since the derived class still calls the base function, the output of this particular example is:

Code: [Select]
Derived
Base

In my real world example, that's exactly what I want to happen. In my theoretical query, how would I have the base class reference its own function and not the derived overridden version?


(In before recursivelyenumerable)

recursivelyenumerable

  • you might think that; I couldn't possibly comment
  • Senior Member
Re: Theoretical OOP Question
« Reply #1 on: September 12, 2008, 09:19:18 PM »
allowing the "this" pointer to be overriden by derived classes at runtime, a.k.a. open recursion, is the essence of OO implementation inheritance.  if you don't want this behavior, don't use implementation inheritance --- just incorporate a "base class" object as a member of your "derived class"
QED

Bocsius

  • is calmer than you are
  • Senior Member
Re: Theoretical OOP Question
« Reply #2 on: September 12, 2008, 09:46:57 PM »
OK, so under no circumstances can an inherited base class directly use its own function once it has been overridden, is that correct? The derived class can achieve that result by using the base keyword, but the base class is essentially oblivious to it. Casting "this" to the base within the base won't even achieve it, as it turns out. You need an independent, non-inherited instance of the base, a base reference within the derived, or a non-overridden function. Does that about cover it?

That's the behavior I was looking for when I first wrote the code this afternoon, I was just halfway expecting it not to work. And I'm not sure why at this point, as it wasn't the first time I've used the "this" keyword in a base to refer to an implementation from a derived class. I guess the only difference is that it has typically been for an abstract method or property where there is no competing base implementation.
« Last Edit: September 12, 2008, 09:48:54 PM by Bocsius »

Bocsius

  • is calmer than you are
  • Senior Member
Re: Theoretical OOP Question
« Reply #3 on: September 12, 2008, 09:47:23 PM »
.

recursivelyenumerable

  • you might think that; I couldn't possibly comment
  • Senior Member
Re: Theoretical OOP Question
« Reply #4 on: September 12, 2008, 10:52:19 PM »
Quote
OK, so under no circumstances can an inherited base class directly use its own function once it has been overridden, is that correct?

Some languages might supply a way, I'm not 100% sure offhand C# doesn't (pretty sure, though).  I just think if you find yourself wanting to do that, there's a good chance you shouldn't have used implementation inheritance to begin with --- working in an "OO language" doesn't mean inheritance is always the appropriate mechanism for reusing functionality.
QED

Bocsius

  • is calmer than you are
  • Senior Member
Re: Theoretical OOP Question
« Reply #5 on: September 12, 2008, 10:55:47 PM »
Thank you for your input. It was a theoretical exercise anyway, I have the behavior I want. But it's nice to learn things, experiment, and learn more.