Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't define static methods in abstract types. How to work arround? [closed]

Tags:

c#

I am currently developping a library for a binary application protocol. The following code sample does not compile (static methods definition not allowed in abstract type. Too bad :(), but shows my intention :

public abstract class CmdBody<T>
{
    public static abstract byte[] ToBytes(T cmdBody);
    public abstract int GetLength();
}

public class CmdBodyA : CmdBody<CmdBodyA>
{
    static override byte[] ToBytes(CmdBodyA cmdBody) 
    { // Implementation }
}

public class CmdBodyB : CmdBody<CmdBodyB>
{
    static override byte[] ToBytes(CmdBodyB cmdBody)
    { // Implementation }
}

[...]

public class Cmd<T>
{ 
    public CmdHeader Header { get; set; }
    public CmdBody<T> Body { get; set; }

    public byte[] ToBytes()
    {
        byte[] cmdBytes = new byte[Header.GetLength() + Body.GetLength()];
        Header.ToBytes().CopyTo(cmdBytes, 0);
        Body.ToBytes().CoptyTo(cmdBytes, Header.GetLength());

        return cmdBytes;
    } 
}

So pretty basic stuff, a Cmd consists of an Header and a Body, Header type being common to all Cmd(s) and Body having different parameters (properties) depending of the Cmd type, and I would like to work with Cmd objects, and to be able to call ToBytes() on them to send them over the network.

In my real implementation I am using conversion operator instead of ToBytes() method, but I wanted to keep the code sample simple, at the end it does the same job.

I have many different command types, and I can't work out a solution to keep it simple and achieving what I want with just a single generic Cmd type. The only solution I can think of would be to get rid of the static method in the base CmdBody class, get rid of the generic aspect and for each CmdBody type make an associated Cmd class (class CmdA, class CmdB ...) but this will incur a lot of code duplication at the end of the day.

Any idea for a nice desing to help me work arround this ?

like image 339
darkey Avatar asked Aug 03 '12 18:08

darkey


People also ask

Why static methods Cannot be declared abstract?

A static method belongs to class not to object instance thus it cannot be overridden or implemented in a child class. So there is no use of making a static method as abstract.

Can we define static methods in abstract class?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.

Can we declare static variable in abstract method?

In Java, a static method cannot be abstract. Doing so will cause compilation errors.

Can abstract class have non static methods?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.


1 Answers

You can't have "virtual" static methods, as virtual/abstract/override suggests an instance being overridden at runtime.

You could do something like this, however:

public abstract class CmdBody
{
    public static byte[] ToBytes<T>(T cmdBody) where T : CmdBody
    {
         return cmdBody.ToBytes();
    }

    protected abstract byte[] ToBytes();

    public abstract int GetLength();
}

public class CmdBodyA : CmdBody
{
    protected override byte[] ToBytes() 
    { // Implementation }
}

public class CmdBodyB : CmdBody
{
    protected override byte[] ToBytes() 
    { // Implementation }
}

This allows each "CmdBody" type to define how it's serialized itself, but the base class static method to be the only publicly visible way to access it.

That being said, given your use case, there's no reason for making this static at all. You can just do:

public abstract class CmdBody
{
    public abstract byte[] ToBytes();

    public abstract int GetLength();
}

public class CmdBodyA : CmdBody
{
    public override byte[] ToBytes() 
    { // Implementation }
}

public class CmdBodyB : CmdBody
{
    public override byte[] ToBytes() 
    { // Implementation }
}


public class Cmd<T> where T : CmdBody
{ 
    public CmdHeader Header { get; set; }
    public T Body { get; set; }

    public byte[] ToBytes()
    {
        byte[] cmdBytes = new byte[Header.GetLength() + Body.GetLength()];
        Header.ToBytes().CopyTo(cmdBytes, 0);
        Body.ToBytes().CopyTo(cmdBytes, Header.GetLength());

        return cmdBytes;
    } 
}
like image 199
Reed Copsey Avatar answered Oct 06 '22 23:10

Reed Copsey