Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. the big Chaos.....

the big Chaos.....

Scheduled Pinned Locked Moved C#
question
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DasdaDAS
    wrote on last edited by
    #1

    Can any one explain when to use abstract class and when to use interface? Or under which scenario we should use abstract class and interface..... Zxczc

    S D 2 Replies Last reply
    0
    • D DasdaDAS

      Can any one explain when to use abstract class and when to use interface? Or under which scenario we should use abstract class and interface..... Zxczc

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Generally, abstract classes are used when you have some implementation in the base class, which calls virtual methods that can be overridden in derived classes. Interfaces can't have implementation, so they can't be used there. Also, interfaces can be used to express "can also be" kind of relationship. For eg, you can have a concrete class deriving from an abstract class and implementing IDisposable, so your class "can be" disposed. Also, there are versioning issues. You can add additional virtual methods to abstract classes without breaking existing clients, you can't do that with interfaces. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      P 1 Reply Last reply
      0
      • D DasdaDAS

        Can any one explain when to use abstract class and when to use interface? Or under which scenario we should use abstract class and interface..... Zxczc

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        Also, to add to the excellent Kumar answer, I'd add that formally, you should use an abstract class when the concrete class you'll implement is an specialization of the abstract one (aka "is a" relationship). When you define an interface, you often model an specific behavior (aka "-able" suffix: IDisposable, IClonable, IMovable, IComparable, IFormattable). I see dead pixels Yes, even I am blogging now!

        R 1 Reply Last reply
        0
        • S S Senthil Kumar

          Generally, abstract classes are used when you have some implementation in the base class, which calls virtual methods that can be overridden in derived classes. Interfaces can't have implementation, so they can't be used there. Also, interfaces can be used to express "can also be" kind of relationship. For eg, you can have a concrete class deriving from an abstract class and implementing IDisposable, so your class "can be" disposed. Also, there are versioning issues. You can add additional virtual methods to abstract classes without breaking existing clients, you can't do that with interfaces. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          P Offline
          P Offline
          Paul Lyons
          wrote on last edited by
          #4

          Awesome answer/explaination! :)

          Paul Lyons, CCPL
          Certified Code Project Lurker

          1 Reply Last reply
          0
          • D Daniel Turini

            Also, to add to the excellent Kumar answer, I'd add that formally, you should use an abstract class when the concrete class you'll implement is an specialization of the abstract one (aka "is a" relationship). When you define an interface, you often model an specific behavior (aka "-able" suffix: IDisposable, IClonable, IMovable, IComparable, IFormattable). I see dead pixels Yes, even I am blogging now!

            R Offline
            R Offline
            Roger Alsing 0
            wrote on last edited by
            #5

            and INativeWindowable ;) (ok ok, I do agree with the above, but as always , there are scenarios when interfaces are used in the "is a" kind of way) //Roger

            D 1 Reply Last reply
            0
            • R Roger Alsing 0

              and INativeWindowable ;) (ok ok, I do agree with the above, but as always , there are scenarios when interfaces are used in the "is a" kind of way) //Roger

              D Offline
              D Offline
              DasdaDAS
              wrote on last edited by
              #6

              Great.. Zxczc

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups