Object Oriented Specific Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rhaazy

    Object Oriented Specific Question

    Why would I ever use an interface when I could use an abstract class?

    Assuming the only limitation is only having one base class, would this
    change assuming you could implement multiple inheritance (and
    guarantee not creating ambiguity issues)?

    Please share your thoughts or explain something to me if I seem to be
    confused.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Object Oriented Specific Question

    Well, in .NET, the fact that you can have only one base class means that
    you are better off (usually) using multiple interface implementations , as
    you don't want to "burn the base class" (because the functionality that the
    implementer wants to expose might be buried somewhere that is not accessible
    to the abstract class or classes that derive from it).

    If you could implement multiple inheritance, then it wouldn't be such an
    issue, but whether or not it would be productive, or cause more problems
    than it solves is a different conversation completely.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "rhaazy" <rhaazy@gmail.c omwrote in message
    news:dd609a33-a25c-40c0-b582-0c1db372472a@l6 4g2000hse.googl egroups.com...
    Why would I ever use an interface when I could use an abstract class?
    >
    Assuming the only limitation is only having one base class, would this
    change assuming you could implement multiple inheritance (and
    guarantee not creating ambiguity issues)?
    >
    Please share your thoughts or explain something to me if I seem to be
    confused.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Object Oriented Specific Question

      rhaazy <rhaazy@gmail.c omwrote:
      Why would I ever use an interface when I could use an abstract class?
      >
      Assuming the only limitation is only having one base class, would this
      change assuming you could implement multiple inheritance (and
      guarantee not creating ambiguity issues)?
      That's not the only limitation, although it's a significant one. (C#
      just *doesn't* support multiple base classes, and is unlikely to ever
      do so). It's also easier to mock out interfaces than classes - it tends
      to be better supported in tools.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon_skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • Chakravarthy

        #4
        Re: Object Oriented Specific Question

        By this time, if you have understood the importance between abstract class
        and interface, I don't have to throw light on them.

        But, thought to place some code, so that it would be meaningful for you
        understand how each will behave and their respective advantages.
        ---------------------------------------------------------
        public interface TopInterface
        {
        void DoSome();
        }
        public abstract class TopAbsClass
        {
        public TopAbsClass()
        {
        cs.WriteLine("F rom Private Constructor..") ; // You have the
        flexibility to embed your code here, which will execute every time the
        inherited class initialized
        }
        public abstract void DoSomeWork();
        }
        public class FirstLevelInher itanceClass : TopAbsClass, TopInterface
        {
        public override void DoSomeWork() // this method is from Abstract
        Class
        {
        Console.WriteLi ne("Printed from FirstLevelInher itanceClass -
        DoSomeWork() Method is executed");
        }
        public void DoSome() // This method is from the interface
        {
        Console.WriteLi ne("Printed from FirstLevelInher itanceClass -
        DoSome() Method is executed");
        }
        }
        ---------------------------------------------------------

        It is recommended to use interfaces, only due to "no code but template".
        Hope this helps you




        "rhaazy" <rhaazy@gmail.c omwrote in message
        news:dd609a33-a25c-40c0-b582-0c1db372472a@l6 4g2000hse.googl egroups.com...
        Why would I ever use an interface when I could use an abstract class?
        >
        Assuming the only limitation is only having one base class, would this
        change assuming you could implement multiple inheritance (and
        guarantee not creating ambiguity issues)?
        >
        Please share your thoughts or explain something to me if I seem to be
        confused.

        Comment

        • Pavel Minaev

          #5
          Re: Object Oriented Specific Question

          On Jul 9, 9:46 pm, rhaazy <rha...@gmail.c omwrote:
          Why would I ever use an interface when I could use an abstract class?
          >
          Assuming the only limitation is only having one base class, would this
          change assuming you could implement multiple inheritance (and
          guarantee not creating ambiguity issues)?
          If you have a purely abstract class (one with all members abstract),
          then there's no design difference from an interface (all limitations
          of C# and .NET aside).

          If you don't have a pure abstract class, and assuming that you would
          have multiple inheritance with no restrictions, then it is still good
          to remeber that implementation inheritance (= extending classes in C#)
          is considered to be a much tighter coupling than contract inheritance
          (= implementing interfaces in C#). This is described in more details
          in a rather well-known "'extends' is evil" article here (written for
          Java, but fully applicable to C# as well):

          Business technology, IT news, product reviews and enterprise IT strategies.

          Comment

          Working...