Question

I have a superclass with two subclasses. The two subclasses both have a method with checks whether a chapter has content. For subclass 1 this method is HasContent(int chapterID) and for subclass 2 this is HasContent(int chapterID, int institution). As you can see subclass 2 has an extra parameter. The purpose of both methods is the same.

I was thinking to put the method HasContent in the superclass. Do you think i need to do this? If so, how should i implement this? Or is it more wisely to put both methods in their own subclass?

EDIT:

The body of HasDocuments looks like this: Subclass1:

Database DB = new Database();
int res = DB.ExecuteSpRetVal(chapterID, mInstitutionID);

if (res > 0)
    return true;
else
    return false;

Subclass2:

Database DB = new Database();
int res = DB.ExecuteSpRetVal(chapterID);

if (res > 0)
    return true;
else
    return false;
Was it helpful?

Solution

Edit: Updated according to the question update.

Since you are clearly having almost the same logic in both methods, I'd refactor it like this:

abstract class SuperClass
{
    protected bool HasContentImpl(int chapterID, int institution)
    {
        Database db = new Database();
        int result;

        if (institution >= 0) // assuming negative numbers are out of range
            result = db.ExecuteSpRetVal(chapterID, institution);
        else
            result = db.ExecuteSpRetVal(chapterID);

        return result > 0;
    }
}

class SubClass1 : SuperClass
{
    public bool HasContent(int chapterID)
    {
        return base.HasContentImpl(chapterID, -1);
    }
}

class SubClass2 : SuperClass
{
    public bool HasContent(int chapterID, int institution)
    {
        return base.HasContentImpl(chapterID, institution);
    }
}

OTHER TIPS

Use method overloading by placing two identically named methods with different arguments into the superclass. When you call HasContent, it will use whichever one matches the number and types of arguments you have provided. Because it is in the superclass, you now won't have to make yet another copy of it if you decide later to make a new subclass that uses this method as well. See below for example code;

protected bool HasContent(int chapterID, int institution)
{
    Database db = new Database();
    int result;

    result = db.ExecuteSpRetVal(chapterID, institution);

    return result > 0;
}

protected bool HasContent(int chapterID)
{
    Database db = new Database();
    int result;

    result = db.ExecuteSpRetVal(chapterID);

    return result > 0;
}

You said:

The purpose of both methods is the same

so yes, it does sounds that you've got a common method that you can put in the superclass.

As HasContent() takes different augments I would not move it up to the base class, the fact that two methods are called the same, does not mean they do the same thing.

I don’t know your code base or what the system you are working on does, however given the little information I have, some thing about your design feels wronge. I have found in the past that often when I have this sort of design problem, it is due to a problem else where in how the data is model. Sorry not match help if you can’t change the rest of the system….

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top