3

I am wondering whether there is any feature like method inheritance rather than whole class inheritance, let me elaborate what I am trying to explain :

class a {
   public void GetA(){
     // some logic here
}
}

class b {
    public void GetB() : new Class().GetA()
}

I know it looks weird but I was reading how to do delegation in object composition pattern and I thought this pattern for some reason.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • Actually I not am asking whether the same way exists, maybe there is another way of doing this in C# which provides better and efficient solution. But the whole class inheritance is not what I am looking for. – Tarik Dec 29 '09 at 02:48
  • Are there any languages that implement this? – GrayWizardx Dec 29 '09 at 02:49
  • @GrayWizardx sure, this type of thing is possible in a lot of languages that have functions as first class objects. I know Python and JavaScript allow you to do this sort of thing. – TM. Dec 29 '09 at 02:50
  • Do you want GetB() to call GetA(), or change GetA()'s behavior? – Aaron Daniels Dec 29 '09 at 02:51
  • @TM do they **actually** let you do it? or do you just get compiler semantics for GetB() { return (new Class()).GetA(); }, I ask because inheritance wise, and usage wise there is a big difference between "method inheritance" and "virtual method invocation", consider the side effects if "a" had static initialization or runtime setup. – GrayWizardx Dec 29 '09 at 03:02
  • Are there any strongly-typed, compiled languages that have first-class methods? – Jason Jackson Dec 29 '09 at 03:47
  • @Aaron Daniels : Actually I want make `GetB()` do the same thing as `GetA()` like `void GetA(){new a().GetA();}` – Tarik Dec 29 '09 at 03:52

7 Answers7

4

A common way to do composition is to create a private member variable in class b, of type class a, and in GetB() call a.GetA(). For example:

class a {
   public void GetA(){
     // some logic here
   }
}

class b {
    private a _a=new a();

    public void GetB()
    {
         _a.GetA();
    } 
}

Another option might be to define a delegate member variable called GetB instead of simple method and allow the calling code to supply the code that is executed by GetB().

Ash
  • 60,973
  • 31
  • 151
  • 169
  • Could you tell me what the benefits are of doing this pattern ? Thank you. – Tarik Dec 29 '09 at 03:53
  • @Aaron, the benefits of this are the same as the benefits of using composition in general. It allows you to hide more details about your implementation then inheritance does. In this example callers of class B do not need to know that you are actually using class A to implement GetB(). So it is generally often more flexible and loosely coupled than inheritance would be. – Ash Dec 29 '09 at 04:02
4

If you just want to call GetA() inside of GetB(), but don't want to define or explicitly reference an instance of class a in GetB(), you can pass GetA() in as a delegate.

In C#, there are many predefined delegates such as Action and Func. Or, you could always roll your own delegate to match the method signature.

    class a
    {
        public void GetA()
        {
            Console.WriteLine("Hello World!");
        }
    }

    class b
    {
        // No explicit reference to class a of any kind.
        public void GetB(Action action)
        {
            action();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var a = new a();
            var b = new b();

            b.GetB(a.GetA);
        }
    }
Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
  • Wow, that looks cool, I didn't know such a Generic solution .net already provides, I was always creating my own delegates... – Tarik Dec 29 '09 at 03:57
3

The closest thing I can think of is how in C# you can "inherit" a class's constructor in that of its child class (or an overloaded constructor of the same class), like so:

class Animal {
    public string Species { get; set; }

    public Animal(string species) {
        Species = species;
    }
}

class Human : Animal {
    public string Name { get; set; }

    public Human(string name) : base("Homo sapien") {
        Name = name;
    }
}

The behavior of the above code is pretty straightforward: the constructor for the Human class essentially calls the constructor for the Animal class before doing anything else. Why this same functionality isn't available to non-constructor methods, I'm not sure.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • @Aaron: Yes, in the `Human` constructor, "Home sapien" gets passed as the 'species' argument to the `Animal` constructor. – Dan Tao Dec 29 '09 at 16:37
1

This is not possible. If you want b.GetB to reuse the functionality of a.GetA then you need to instantiate an a and invoke a.GetA.

jason
  • 236,483
  • 35
  • 423
  • 525
0

A slightly different approach would be to accept an instance of ClassA in the constructor of ClassB, rather than instantiating it directly. This would be applying the Dependency Inversion Principle to @Ash's answer:

public class ClassB
{
    private readonly ClassA _a;

    public b(ClassA a)
    {
        _a = a;
    }

    public void GetB()
    {
        _a.GetA();

        // Other logic
    }
}
Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
0
public class Article
{

 public object AddOrUpdate(params object[] paras){

   return null;

  }

}

public class Test:Article
{

 public new object AddOrUpdate(params object[] paras){

   //do your logic......

    return base.AddOrUpdate(paras);

 }


}

you mean this?this is inhret a class can do get or set BASE object property.

if you do with delegate ,you must do more job with the same logic.

but delegate can get the diffenent domain object and do more over-app thing.

cjjer
  • 53
  • 7
0

If GetA() is changed to a static method, you can simply call it within the GetB() function:

class a { 
   public static void GetA() { 
     // some logic here 
   } 
} 

class b { 
    public void GetB() {
      a.GetA();
    }
} 

If GetA() is not static, it doesn't make sense since GetA() needs an object context (eg., the invisible "this" pointer) by definition. You can't pass an instance of object B to class A, as class A does not know anything about class B.

What are you really attempting to do?

tgiphil
  • 1,242
  • 10
  • 22