Calling a method from base class to class with same name as the method

Lost Syndicate

Summary: B Receives call from A, and automatically starts the start method

Let's say we have class A: A has a function named Start, that start function is initially called from another class (lets refer that class as C) and once it's called, it should call every method from all classes using Start (and using it's base class), with the same method.

And we have B: a class using A as it's base class, it's job is to receive the Start method from A, (initially called by C). Now i could simply do this by calling B's method directly from A, but in this case, B could be named anything, with multiple classes inheriting the same method.

This is useful because i don't want to assign all my variables to one start function. Instead, be able to create a function that allows the same function name to be called.

For example: here's what i was thinking it would look like

class A
{
    public void Start()
    {
        // Call B's Start here...
    }
}

class B : A
{
    public void Start()
    {
        // Receive A's call ... and do stuff here
    }
}


// This time, we also need to access this start method.
// But this class could be named anything
class anotherClass : A
{
    public void Start()
    {
        // Receive A's call
    }
}

class C
{
    static void Main(string[] args)
    {
        A a = new A();

        // Maybe somehow call all start methods here
        a.Start();
    }
}

But as you can see, Start in class A will be called, but it will never call start in class B.

In better context, I need a way for all Start methods from every class to be called.

Olivier Jacot-Descombes

You have never created a B or anotherClass object. Therefore their Start method cannot be called.

Inheritance works the other way round. A derived class can call its base class members, because it knows its ancestor and inherits all its members (fields, properties, methods). The base class (A) on the other side does not know its descendants.

You must use virtual methods that you can override in derived classes. Example:

class A
{
    public virtual void Start()
    {
        Console.WriteLine("Starting A");
    }
}

class B : A
{
    public override void Start()
    {
        base.Start();
        Console.WriteLine("Starting B");
    }
}

Now you can create a B object and call its Start method

var b = new B();
b.Start();

This will output:

Starting A
Starting B

Since derived types are assignment compatible to their base types, you can also do something like this

var list = new List<A> { new B(), new A() };
foreach (A a in list) {
    a.Start();
}
Starting A
Starting B
Starting A

where the two first lines are from B.Start() and the last one from A.Start().


But this works only for classes in the direct lineage. You cannot call methods from siblings. Why? Let's make an example:

class C : A
{
    private string s = "hello";

    public override void Start()
    {
        base.Start();
        Console.WriteLine("Starting C: " + s);
    }
}

Assuming that you could do something like this in B:

sibling(C).Start();

Where should the value "hello" of s come from? Neither A, nor B has such a field and a C object was never created. So your requirement to call every method from all classes cannot be fulfilled. But if a field of A was involved this would work, as B inherits this field.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling method of same name from different class

From Javascript

Calling a method from another method in the same class

From Dev

Calling a Method from "base.base" class?

From Dev

JS - Calling method in class from other method in same class

From Dev

Calling the base class method from Code Quotations

From Dev

Calling a constructor from method within the same class

From Dev

calling a method from same class in xcode

From Java

Calling base class overridden function from base class method

From Dev

Calling a method from a class

From Dev

Why does calling a method from base class calls the child method?

From Java

Calling base class method in Python

From Dev

Calling method of template base class

From Dev

Calling derived class method from base class destructor

From Dev

calling child class method from base class C#

From Dev

Calling a method from a derived class object that returns a base class pointer

From Dev

Calling an extention method of a base class from child class

From Dev

Calling private method from a Public method in the same class

From Dev

javascript why calling a method from another method in the same class need this?

From Dev

Error with calling a method from another method in the same class

From Dev

Calling a method from another method inside the same class using getattr

From Dev

Javascript OOP - calling a method from a different method of the same class

From Dev

Dynamically calling method and class name

From Dev

get a class name of calling method

From Dev

Calling a method within the same class

From Dev

Calling method of the base class (not the immediate parent class)

From Java

Calling a method inside another method in same class

From Dev

@Transactional method calling a method in the same class

From Dev

Calling a method in ruby from a method with the same name

From Dev

Calling base class method after child class __init__ from base class __init__?

Related Related

  1. 1

    Calling method of same name from different class

  2. 2

    Calling a method from another method in the same class

  3. 3

    Calling a Method from "base.base" class?

  4. 4

    JS - Calling method in class from other method in same class

  5. 5

    Calling the base class method from Code Quotations

  6. 6

    Calling a constructor from method within the same class

  7. 7

    calling a method from same class in xcode

  8. 8

    Calling base class overridden function from base class method

  9. 9

    Calling a method from a class

  10. 10

    Why does calling a method from base class calls the child method?

  11. 11

    Calling base class method in Python

  12. 12

    Calling method of template base class

  13. 13

    Calling derived class method from base class destructor

  14. 14

    calling child class method from base class C#

  15. 15

    Calling a method from a derived class object that returns a base class pointer

  16. 16

    Calling an extention method of a base class from child class

  17. 17

    Calling private method from a Public method in the same class

  18. 18

    javascript why calling a method from another method in the same class need this?

  19. 19

    Error with calling a method from another method in the same class

  20. 20

    Calling a method from another method inside the same class using getattr

  21. 21

    Javascript OOP - calling a method from a different method of the same class

  22. 22

    Dynamically calling method and class name

  23. 23

    get a class name of calling method

  24. 24

    Calling a method within the same class

  25. 25

    Calling method of the base class (not the immediate parent class)

  26. 26

    Calling a method inside another method in same class

  27. 27

    @Transactional method calling a method in the same class

  28. 28

    Calling a method in ruby from a method with the same name

  29. 29

    Calling base class method after child class __init__ from base class __init__?

HotTag

Archive