cubesnyc
cubesnyc

Reputation: 1545

C# object oriented class structure

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

Upvotes: 1

Views: 106

Answers (2)

Lloyd
Lloyd

Reputation: 29668

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.

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

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

Upvotes: 0

Related Questions