2
public interface IMyInterface
{
   int A { get; set; } 
   string S { get; set; } 
}

public abstract class MyAbs : IMyInterface
{ }

public class ConcreteClass : MyAbs
{
   /* Implementation of IMyInterface*/
}

Is it possible to omit the empty implementation in an abstract class as above? If so, how? why?

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
user320199
  • 29
  • 3
  • 1
    put the code in a code section – Itay Karo May 28 '10 at 11:13
  • If your abstract class is going to be totally devoid of implementations, then why have it at all? Why not use the interface? Abstract classes are generally for cases where you provide a partial implementation. – David M May 28 '10 at 11:15

2 Answers2

2

you can define the methods/properties as abstract in the abstract class so it won't have empty implementation

The code you provided will not compile

what you need is

    public interface IMyInterface
    {
        int A
        { get; set; }
        string S
        { get; set; }
    }
    public abstract class MyAbs : IMyInterface
    {
        public abstract int A { get; set; }
        public abstract string S { get; set; }
    }
    public class ConcreteClass : MyAbs
    {
        /* Implementation of IMyInterface*/
    }
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
  • http://stackoverflow.com/questions/197893/why-an-abstract-class-implementing-an-interface-can-miss-the-declaration-implemen Pls refer to this link – user320199 May 28 '10 at 11:17
  • Java allows abstract classes to omit interface methods decelerations but c# does not. – Itay Karo May 28 '10 at 11:30
0

The purpose of an abstract class is to encapsulate some common behaviour that applies to all the deriving classes. If you haven't got anything at all in your abstract class then it there is no purpose to having it, ConcreteClass should implement the interface directly.

If you subsequently add another class that also implements this behaviour, then that is the time to refactor and create an abstract base class if it makes sense.

Paolo
  • 22,188
  • 6
  • 42
  • 49
  • http://stackoverflow.com/questions/197893/why-an-abstract-class-implementing-an-interface-can-miss-the-declaration-implemen Pls have a look at this link – user320199 May 28 '10 at 11:19
  • 1
    a) That's Java not C#, In C# you have to declare the methods as abstract, b) It doesn't alter the fact that a completely abstract class isn't a useful thing in this context – Paolo May 28 '10 at 11:23
  • Nope, you must implement the method or declare it abstract – Paolo May 28 '10 at 11:26