invoke ancestor method

how to jump over the method in the superclass

Page 1 of 1

6 Replies - 2242 Views - Last Post: 26 June 2009 - 01:42 PM Rate Topic: -----

#1 liz hundy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-June 09

invoke ancestor method

Post icon  Posted 25 June 2009 - 07:34 AM

Hello All,

Sorry to keep bothering you with trivial questions but ...

How do I call a method in an ancestor class when there is a method already in the immediate superclass?

For example
class C extends class B, class B extends class A, and all three classes have copy() method.

Now, from class C, I want to use the copy method of class A.
like
base.base.copy( anObject);
but it does not work. ;-(

---Liz

Is This A Good Question/Topic? 0
  • +

Replies To: invoke ancestor method

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: invoke ancestor method

Posted 25 June 2009 - 08:11 AM

An easy way would be to create a method in class B to access class A.

Here's another possible way.

class A
{
    protected void AMethod()
    {
        // AMethod code
    }

    public virtual void Method()
    {
        AMethod();
    }
}
class B : A
{
    protected void BMethod()
    {
        // BMethod code
    }

    public virtual void Method()
    {
        BMethod();
    }
}

class C : B
{
    protected override void Method()
    {
        AMethod();
    }
}



Hope this helps!

This post has been edited by lesPaul456: 25 June 2009 - 08:12 AM

Was This Post Helpful? 1
  • +
  • -

#3 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: invoke ancestor method

Posted 25 June 2009 - 11:05 AM

you could use reflection.
base.GetType().GetMethod("copy").Invoke();


but one of the parameters would be a object[], that where you would pass data to the method. I'm know sure if it will work cause I'm not at home at the moment.
Was This Post Helpful? 0
  • +
  • -

#4 liz hundy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-June 09

Re: invoke ancestor method

Posted 25 June 2009 - 02:54 PM

View PostlesPaul456, on 25 Jun, 2009 - 07:11 AM, said:

An easy way would be to create a method in class B to access class A.


Thanks, I will use something like that.

Would I be right in assuming that it is not possible to specify at which position in the class hierarchy the search for an inherited method should start?

---Liz
Was This Post Helpful? 0
  • +
  • -

#5 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: invoke ancestor method

Posted 25 June 2009 - 04:10 PM

Sure you can:

using System;

namespace Inheritance
{
    public class A
    {
        public A()
        {
        }
        public virtual void Copy(Type t, string file)
        {
            if (typeof(A) == t)
                Console.WriteLine("Type matched in class A");
            else
                Console.WriteLine("Type does not belong to this family tree.");
            return;
        }
    }
    public class B : A
    {
        public B()
        {
        }
        public override void Copy(Type t, string file)
        {
            if (typeof(B ) == t)
                Console.WriteLine("Type matched in class B");
            else
                base.Copy(t, file);
            return;
        }
    }
    public class C : B
    {
        public C()
        {
        }
        public override void Copy(Type t, string file)
        {
            if (typeof(C ) == t)
                Console.WriteLine("Type matched in class C");
            else
                base.Copy(t, file);
            return;
        }
    }
}



Here's the driver(program):

using System;

namespace Inheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            C child = new C();
            child.Copy(typeof(A), String.Empty);
            child.Copy(typeof(C ), String.Empty);
            child.Copy(typeof(B ), String.Empty);
            child.Copy(typeof(string), String.Empty);
            Console.ReadLine();
        }
    }
}



All calls to Copy() will print out a status. The first 3 successfully call the corresponding Copy() method, but the last one fails because the string class is not in the hierarchy.

Why do you need such a requirement anyway? We may be able to suggest a different design if you post your code or explain what you're program is supposed to do.

Note: I know you think it looks weird, but I had to add a whitespace in the typeof calls between the C and ) and the B and ), otherwise you would see smiley faces in the code.
Was This Post Helpful? 1
  • +
  • -

#6 liz hundy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-June 09

Re: invoke ancestor method

Posted 26 June 2009 - 05:03 AM

View Postmasteryee, on 25 Jun, 2009 - 03:10 PM, said:

Sure you can:

Why do you need such a requirement anyway? We may be able to suggest a different design if you post your code or explain what you're program is supposed to do.


Thank you very much for that illuminating example.

You are right, I do not really want to do that - but it is nice to know how.

I have a hierarchy of classes, and by using a mixture of overloading and overriding the copy method I was hoping to be able to do something sensible with any pair of objects.

I just have one awkward class, where I do not want to use the copy method of the superclass, but I do want to copy the bits that are inherited from its grandparent. This happens because sometimes I want to create new members and other times I just want to copy the reference. Sometimes objects "own" the member, sometimes they reference an external object that exists in its own right and can be shared. Bad design?

---Liz
Was This Post Helpful? 0
  • +
  • -

#7 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: invoke ancestor method

Posted 26 June 2009 - 01:42 PM

It's not necessarily a bad design, but you may need to reconsider if an "is-a" relationship really exists between the "awkward" class and its parent class because it sounds more like the "awkward" class has an "is-a" relationship with the "grandparent" class. A Circle is an Ellipse, but an Ellipse is not a Circle because even though both methods can draw a curve, all points in an Ellipse are not required to be equidistant from its center like a Circle. A DesktopPC is a Computer, and a Laptop is a Computer, but even though a Laptop has similar functionality as the DesktopPC, you can't say a LapTop is a DesktopPC or vice versa for the sake of this example.

Aside from that, are you saying that all you want to do is copy the settings of an object of class A into an object of class C? You can create a constructor to do this if that's what your intent is.

Here's are some variables and constructors for class A:

        protected string name; // allow the child classes to use this variable without redeclaring it

        public string Name // publicly accessible property
        {
            get
            {
                return name;
            }
        }

        public A(string inName)
        {
            name = inName;
        }
        public A()
        {

        }



Here's how class C's constructor can make use of this new code:

        public C(A familyObject)
        {
            name = familyObject.Name;
        }
        public C(string inName)
        {
            name = inName;
        }
        public C()
        {
        }



program:

            A topClass = new A("Top");
            B subClass = new B("Sub");
            C lowClass = new C("Low");
            C awkwardClass = new C(topClass);
            topClass.DisplayName(); // displays "Top"
            subClass.DisplayName(); // displays "Sub"
            lowClass.DisplayName(); // displays "Low"
            awkwardClass.DisplayName(); // displays "Top"


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1