-1

I have an abstract class but and 6 different implementations of it. Now for one of them I would like to add an extra public method but would not like to add it to the abstract class because the other 5 implementations don't need it.

Is there a way of implementing this? I am getting an error when I add this new method without it being in the abstract class.

Here is the code:

namespace Results.Parser
{
public abstract class Parser<T> where T: ReportItem
    {
        public abstract string GetNodeName();
        public abstract IEnumerable<ReportItem> ParseXml(TextReader stream);
        public abstract List<ReportItem> SumValueOfDuplicateRows(List<T> reportList);

        public virtual bool MeetsCriteria(ReportItem reportItem)
        {
            return reportItem.Value.SafeConvertToDecimal() != 0;
        }
    }
}

public class ElementParser : Parser<ReportItem>
    {

        public override string GetNodeName()
        {
            return "Element";
        }

        public override List<ReportItem> SumRiskValueOfDuplicateRows(List<ReportItem> reportList)
        {
        // do something 
        }

public void SerializeXml(TextReader stream)
        {
        //new method which is not in abstract class
        }
public override IEnumerable<ReportItem> ParseXml(TextReader stream)
        {
        //do something
        }
}

namespace Results.Producer
{
public class RepositoryManager

{
 private void GetResponse(TextReader stream)
   {
     var parser = new ElementParser();

     parser.SerializeXml(stream);

    }

}
user3710760
  • 557
  • 1
  • 4
  • 17
  • 11
    Show your code and tell us what the error is. We can't debug code we can't see. – tnw Jun 30 '15 at 15:02
  • 2
    You can add extra methods to an abstract class but you won't be able to access them from an object declared as the abstract class, even if the instance has the method. Without the code, there's not much to go on. – Holloway Jun 30 '15 at 15:03
  • 1
    @tnw This was never a debugging question. If you had looked at it closely, you would have noticed that all the required information was present in the question even before my edit. Try improving a question. Not everyone is a native English speaker. – Chetan Kinger Jun 30 '15 at 15:40
  • @Trengot *You can add extra methods to an abstract class but you won't be able to access them from an object declared as the abstract class*. I believe you meant a reference of the abstract class? – Chetan Kinger Jun 30 '15 at 15:43
  • @ChetanKinger, yeah, that's the one I was after. – Holloway Jun 30 '15 at 15:46
  • @user3710760 This is not eve a Java question. – Chetan Kinger Jun 30 '15 at 15:51
  • @ChetanKinger sorry, updated it now – user3710760 Jun 30 '15 at 15:53
  • @user3710760 No problem. Take a look at my answer. I believe it's a language agnostic answer anyway. – Chetan Kinger Jun 30 '15 at 15:59
  • @ChetanKinger It's not a debugging question, but OP claims an error and then adds code demonstrating the problem? What? What does this even have to do with non-native english speakers? – tnw Jun 30 '15 at 16:12
  • @ChetanKinger It was originally a Java question. See the edit history. But would apply to OO general I guess. – Ely Jun 30 '15 at 22:37
  • @user3710760 Not sure why you changed the tag from java to c#, but well. Did any of the answers prove helpful to your case ? – Ely Jun 30 '15 at 22:40
  • It was supposed to be a general OO question - hence no code. – user3710760 Jul 01 '15 at 09:05
  • Then people complained about no code so I added my C# code but forgot to change the tag to Java. The answer marked as correct is the helpful one. – user3710760 Jul 01 '15 at 09:06

1 Answers1

0

I am getting an error when I add this new method without it being in the abstract class.

A superclass reference to a subclass instance can't access methods not defined in the super class. You will need to explicitly cast your reference to a subclass reference type to be able to access the extra method from the subclass.

Let A be the abstract class and let B be the class where you have an extra method called extra. Let a be a reference of type A to an instance of type B. To access the extra method in B, do this :

((B)a).extra();

That being said, if only one of the classes needs additional behavior that is different, you should consider using composition over inheritance. See this answer that explains the famous duck problem that is similar to the situtation you are in currently

Community
  • 1
  • 1
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82