views:

2142

answers:

4

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;
A: 

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.

ninj
+5  A: 

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);
    }
}
Hosam Aly
So you suggest to put the body in the subclasses? And why is public bool HasContent(int chapterID, int institution) public?
Martijn
Yes, I do, unless there is something common in the implementation. I have just seen your edit, so I'll update my answer accordingly.
Hosam Aly
Why is the HasContent overload public? Because I'm assuming you want to call it from other classes! :)
Hosam Aly
Okee, but in SubClass2 public override bool HasContent(int chapterID)may not be called, because institutionId is required.
Martijn
I have updated my answer to match your updated question.
Hosam Aly
Thank you very much :)
Martijn
You're welcome. BTW, unless the methods have something to do with the object itself, you should probably make them static.
Hosam Aly
It would be much easier to just have it in the Super Class by using two overloaded methods. Just place both HasContent methods from the subclasses into the super class and remove the base prefix. If you supply 1 argument when calling it you would get the 1 argument method running, if you use 2 arguments you would get the other running. Have a look here, if you don't know about Method Overloading; http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=105
Stephen Belanger
A: 

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….

Ian Ringrose
A: 

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;
}
Stephen Belanger