1

Suppose a hierarchical structure where B is a parent of C, D

I would like to define a method in B that C, and D will utilize, but it would reference other methods that would be defined in C and D

What is the best approach for that type of structure?

in pseudo code

class B
  int login()
      parseSite()

class C : B
  int parseSite()
      site specific logic goes here

class D : B
  int parseSite()
      site specific logic goes here
2

2 Answers 2

1

What you want is an abstract method, for example:

class abstract B
{

    public int login()
    {
        parseSite();
    }

    protected abstract void parseSite();

}

class C : B
{

    protected override void parseSite()
    {

    }

}

class D : B
{

    protected override void parseSite()
    {

    }

}

The login() method is inherited in all the descendants of B and calls the parseSite() method which must be implemented in any descendant of B.

Sign up to request clarification or add additional context in comments.

1 Comment

do you have to specify whether the abstract method is async?
0

Make parseSite method abstract within B and override it in C and D.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.