locked
abstract class needs to implement interface

    Question

  • Hello, I have an interface and base abstract class defined as follows:

    interface IbusinessInterface
    {
        void DoSth1();
        void DoSth2();
        void DoSth3();
        void DoSth4();
        void DoSth5();
        ...
        ...
        void DoSthN();
    }


    public abstract class BaseClass : IBusinessInterface
    {
        public abstract void DoSth1();
        public abstract void DoSth2();
        public abstract void DoSth3();
        public abstract void DoSth4();
        public abstract void DoSth5();
        ...
        ...
        public abstract void DoSthN();



    what is the reason that this abstract class needs to have methods of the interface "implemented"  ?

    Thanks,
    Michal


    Wednesday, March 11, 2009 6:27 PM

Answers

  • The abstract class is still a class, regardless of whether or not you can directly instantiate it.  It's the C# language equivalent of "This page intentionally left blank".  While technically, the designers of the language could have decided to infer that the user intentionally left out certain methods when inheriting an interface into an abstract class, they chose to cause the developer to purposefully declare that "I know about the method, and I'm intentionally making it abstract".
    David Morton - http://blog.davemorton.net/
    Wednesday, March 11, 2009 6:52 PM

All replies

  • The abstract class is still a class, regardless of whether or not you can directly instantiate it.  It's the C# language equivalent of "This page intentionally left blank".  While technically, the designers of the language could have decided to infer that the user intentionally left out certain methods when inheriting an interface into an abstract class, they chose to cause the developer to purposefully declare that "I know about the method, and I'm intentionally making it abstract".
    David Morton - http://blog.davemorton.net/
    Wednesday, March 11, 2009 6:52 PM
  • David M Morton said:

    The abstract class is still a class, regardless of whether or not you can directly instantiate it.  It's the C# language equivalent of "This page intentionally left blank".  While technically, the designers of the language could have decided to infer that the user intentionally left out certain methods when inheriting an interface into an abstract class, they chose to cause the developer to purposefully declare that "I know about the method, and I'm intentionally making it abstract".


    David Morton - http://blog.davemorton.net/



    Michal, this is not a joke.

    That's a very good analogy.  "This page intentionally left blank."   That perspective is one the keys to the OOP mind set.
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 11, 2009 11:03 PM
  • Yes, I now that this is not a joke :)
    However, as far as i remember, java language, which is object oriented too, does not force developer to do so... and nobody complains that it does not enforce OOP.


    M.
    Wednesday, March 11, 2009 11:08 PM
  • Welcome to C#.  What I like most about it is that it enforces the developer to explicit.  That's how it is.  No more need to know every last paragraph of the language or compiler specification.  It is what it is.  A very refreshing change from the software mindset of past decades when software was not cross platform transportable.

    (Did I just invent a new term?)   :)

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 11, 2009 11:30 PM
  • David M Morton said:

    The abstract class is still a class, regardless of whether or not you can directly instantiate it.  It's the C# language equivalent of "This page intentionally left blank".  While technically, the designers of the language could have decided to infer that the user intentionally left out certain methods when inheriting an interface into an abstract class, they chose to cause the developer to purposefully declare that "I know about the method, and I'm intentionally making it abstract".


    David Morton - http://blog.davemorton.net/



    The method defined in the interface is abstract already. There is no need to declare it abstract for the second time IMHO.
    On the other hand I agree that this is more explicit than without it (as in java), however what am I actually telling the compiler is that the method does not change (remains abstract). In my opinion it is redundant a bit. Also i'm not sure how refactoring tools deal with this (need to check it). In my opinion advantages are not that concrete, unless there are other reasons than "explicitness".  

    Michal
    Wednesday, March 11, 2009 11:47 PM
  • Look at the other side of the coin!  Think interface contract. 
    The abstract base class is NOT defining any default implementation for you. 

    Now, when you browse the abstract class you can SEE the member methods that are or not implemented without having to look any further.  Also, when the members of the abstract class are declared as "abstract" the IDE can generate method stubs for you automatically!  That does not happen if they are declared as "virtual".


    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 11, 2009 11:54 PM