Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an abstract class inherits another abstract class issue

I have an inheritance schema like below:

public abstract class BaseAttachment
{
    public abstract string GetName();
}

public abstract class BaseFileAttachment:BaseAttachment
{
    public abstract string GetName();
}

public class ExchangeFileAttachment:BaseFileAttachment
{
    string name;
    public override string GetName()
    {
        return name;
    }
}

I basically want to call GetName() method of the ExchangeFileAttachment class; However, the above declaration is wrong. Any help with this is appreciated. Thanks

like image 668
stoney78us Avatar asked Nov 27 '12 20:11

stoney78us


2 Answers

The two immediate problems I see is that your final ExchangeFileAttachment class is declared abstract, so you'll never be able to instantiate it. Unless you have another level of inheritance you are not showing us, calling it will not be possible - there's no way to access it. The other problem is that BaseFileAttachment has a property that is hiding the GetName() in BaseAttachment. In the structure you are showing us, it is redundant and can be omitted. So, the 'corrected' code would look more like:

public abstract class BaseAttachment
{
    public abstract string GetName();
}

public abstract class BaseFileAttachment : BaseAttachment
{
}

public class ExchangeFileAttachment : BaseFileAttachment
{
    string name;
    public override string GetName()
    {
        return name;
    }
}

I put corrected in quotes because this use-case still does not make a ton of sense so I'm hoping you can give more information, or this makes a lot more sense on your end.

like image 134
tmesser Avatar answered Sep 17 '22 00:09

tmesser


Just remove the redeclaration from BaseFileAttachment:

public abstract class BaseFileAttachment : BaseAttachment
{
}

BaseFileAttachment already inherits the abstract GetName declaration from BaseAttachment. If you really want to mention it again in BaseFileAttachment, use the override keyword:

public abstract class BaseFileAttachment : BaseAttachment
{
    public override abstract string GetName(); // that's fine as well
}
like image 39
Heinzi Avatar answered Sep 17 '22 00:09

Heinzi