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. Abstract Classes

Abstract Classes

Scheduled Pinned Locked Moved C#
questionlearning
13 Posts 8 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.
  • S Offline
    S Offline
    SPanicker
    wrote on last edited by
    #1

    Hi All, Am just learning things. Can you explain me whether an Interface is a super set to Abstract Classes? In what way Abstract Classes can prove more useful than an interfaces? I know that it is not instantiable. Can you give me a small scenario were Abstract Class will be the only solution rather than using Interfaces?

    Regards, Panicker.

    S P R L 5 Replies Last reply
    0
    • S SPanicker

      Hi All, Am just learning things. Can you explain me whether an Interface is a super set to Abstract Classes? In what way Abstract Classes can prove more useful than an interfaces? I know that it is not instantiable. Can you give me a small scenario were Abstract Class will be the only solution rather than using Interfaces?

      Regards, Panicker.

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Recommendations for Abstract Classes vs. Interfaces[^]


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      S 1 Reply Last reply
      0
      • S SPanicker

        Hi All, Am just learning things. Can you explain me whether an Interface is a super set to Abstract Classes? In what way Abstract Classes can prove more useful than an interfaces? I know that it is not instantiable. Can you give me a small scenario were Abstract Class will be the only solution rather than using Interfaces?

        Regards, Panicker.

        P Offline
        P Offline
        Paul Brower
        wrote on last edited by
        #3

        An interface defines a contract. It contains only signatures of methods, properties, etc. An abstract class can contain implementations, but cannot be instantiated. A good way to determine if you need an abstract class vs. interface would be: If you plan on having derived objects, all of which will need to do some of the same thing, you should use an abstract class. Keep in mind, that abstract classes and interfaces can be used in conjunction where appropriate.

        J L S 3 Replies Last reply
        0
        • P Paul Brower

          An interface defines a contract. It contains only signatures of methods, properties, etc. An abstract class can contain implementations, but cannot be instantiated. A good way to determine if you need an abstract class vs. interface would be: If you plan on having derived objects, all of which will need to do some of the same thing, you should use an abstract class. Keep in mind, that abstract classes and interfaces can be used in conjunction where appropriate.

          J Offline
          J Offline
          jayart
          wrote on last edited by
          #4

          Can we say that Interface is pure virtual form of an abstract class? (similar to OOPS concept) -rt

          G 1 Reply Last reply
          0
          • S SPanicker

            Hi All, Am just learning things. Can you explain me whether an Interface is a super set to Abstract Classes? In what way Abstract Classes can prove more useful than an interfaces? I know that it is not instantiable. Can you give me a small scenario were Abstract Class will be the only solution rather than using Interfaces?

            Regards, Panicker.

            R Offline
            R Offline
            Rahul83
            wrote on last edited by
            #5

            Hi..Let me make it clear to you that interface is not a substitue for abstract class..Abstract class simply act as a base for all the classes that are derived from it(for ex lets say class stundent,class teacher is derived from some class "xyz" which contains feild called organization name,which is reqd by both the derived classes).Since this base class alone itself is of no use so by adding keyword abstract it is restricted from getting Instansiate. Now coming to Interface...since c# does not support multiple inheritance so we can use interface where we will mention method signature only..and the class implementing interface should define that method..now u might be thinking that how interface will be a solution for non supporting multiple inheritance nature of c#..well in a class you can implement multiple interfaces.. Well abstract class and interface are often seen together..reason is that we need not to implement interface for each class separetly..so implement interface for abstract class which acts as a base class and all the classes that derived from this abstract class can make use of interface implemented once for abstract class..

            S 1 Reply Last reply
            0
            • J jayart

              Can we say that Interface is pure virtual form of an abstract class? (similar to OOPS concept) -rt

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              ArtiGujare wrote:

              Can we say that Interface is pure virtual form of an abstract class? (similar to OOPS concept)

              Yes, you could, but I don't know if that would make it less or more confusing... An interface contains no implementation, in much the same way that a pure virtual method doesn't contain any implementation. However, an important difference is that you inherit a class, but you implement an interface. As .NET doesn't have multiple inheritance, a class can only inherit from one base class, but you can make a class implement as many interfaces as you like.

              --- single minded; short sighted; long gone;

              1 Reply Last reply
              0
              • P Paul Brower

                An interface defines a contract. It contains only signatures of methods, properties, etc. An abstract class can contain implementations, but cannot be instantiated. A good way to determine if you need an abstract class vs. interface would be: If you plan on having derived objects, all of which will need to do some of the same thing, you should use an abstract class. Keep in mind, that abstract classes and interfaces can be used in conjunction where appropriate.

                L Offline
                L Offline
                Le centriste
                wrote on last edited by
                #7

                Another example you often see is an abstract class partially implementing an interface. A good example of this is System.Collections.DictionaryBase providing partial implementation of common functionality to System.Collections.IDictionary interface.

                ----- Formerly MP(2) If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

                S 1 Reply Last reply
                0
                • S Stefan Troschuetz

                  Recommendations for Abstract Classes vs. Interfaces[^]


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  S Offline
                  S Offline
                  SPanicker
                  wrote on last edited by
                  #8

                  Thnx Stefan.

                  Regards, Panicker.

                  1 Reply Last reply
                  0
                  • P Paul Brower

                    An interface defines a contract. It contains only signatures of methods, properties, etc. An abstract class can contain implementations, but cannot be instantiated. A good way to determine if you need an abstract class vs. interface would be: If you plan on having derived objects, all of which will need to do some of the same thing, you should use an abstract class. Keep in mind, that abstract classes and interfaces can be used in conjunction where appropriate.

                    S Offline
                    S Offline
                    SPanicker
                    wrote on last edited by
                    #9

                    Paul,thnx fr the clarification.:)

                    Regards, Panicker.

                    1 Reply Last reply
                    0
                    • R Rahul83

                      Hi..Let me make it clear to you that interface is not a substitue for abstract class..Abstract class simply act as a base for all the classes that are derived from it(for ex lets say class stundent,class teacher is derived from some class "xyz" which contains feild called organization name,which is reqd by both the derived classes).Since this base class alone itself is of no use so by adding keyword abstract it is restricted from getting Instansiate. Now coming to Interface...since c# does not support multiple inheritance so we can use interface where we will mention method signature only..and the class implementing interface should define that method..now u might be thinking that how interface will be a solution for non supporting multiple inheritance nature of c#..well in a class you can implement multiple interfaces.. Well abstract class and interface are often seen together..reason is that we need not to implement interface for each class separetly..so implement interface for abstract class which acts as a base class and all the classes that derived from this abstract class can make use of interface implemented once for abstract class..

                      S Offline
                      S Offline
                      SPanicker
                      wrote on last edited by
                      #10

                      Rahul thnx fr ur detailed explanation.:)

                      Regards, Panicker.

                      1 Reply Last reply
                      0
                      • L Le centriste

                        Another example you often see is an abstract class partially implementing an interface. A good example of this is System.Collections.DictionaryBase providing partial implementation of common functionality to System.Collections.IDictionary interface.

                        ----- Formerly MP(2) If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

                        S Offline
                        S Offline
                        SPanicker
                        wrote on last edited by
                        #11

                        Thnx Centriste nd Guffa.:)

                        Regards, Panicker.

                        1 Reply Last reply
                        0
                        • S SPanicker

                          Hi All, Am just learning things. Can you explain me whether an Interface is a super set to Abstract Classes? In what way Abstract Classes can prove more useful than an interfaces? I know that it is not instantiable. Can you give me a small scenario were Abstract Class will be the only solution rather than using Interfaces?

                          Regards, Panicker.

                          L Offline
                          L Offline
                          LogiPro101
                          wrote on last edited by
                          #12

                          Difference between Abstract classes and Interface Little background: A real world entity is represented in an OOP world as object. Objects are defined as classes any language. A class defines objects in two aspects Object's characteristics - Data Members Object's functionality - Member functions So, when the classes that are forced to provide implementations of the functions prototypes that are defined in either ‘interfaces’ or a ‘abstract class’ are not belonging to the same family (having some inherited parental characteristics from same parent directly or indirectly) but are rather related through some functionalities then 100% we will go with interface and if above said classes are belonging to same family then we should go with abstract class inheritance. Also in case of inheritance as everything is a specialization of base parent object so there is allowance of providing some default definitions of the functions that are needed to be implemented further by specialized classes. This is not any way allowed in interfaces because implementing classes are not belonging to same family and are not any way related by characteristics. An Example: We have four real world objects: • Airplane • Bird • Sparrow • Eagle We have one method that is a functionality of all above objects • Fly Now all objects share common functionality flying in air but differ in the mechanism they use to fly. Also by close look we see that in general there is some common characteristics (as well as some common functionalities) between Sparrow and Eagle that can be a part of common class Bird (that has to be abstract because there is no real thing like bird!). But any way Airplane can't be related to Bird or its specialized classes Sparrow and Eagle. So if want to expose fly functionality to outer world and want their implementations in Airplane and Sparrow (or any bird), so I will 100% go for Interface and Abstract classes will be a big NO! But if we exclude Airplane want that fly functionality is exposed to outer world and its implementing classes are Bird, Sparrow and Eagle then 'Abstract Classes' is the best of best choice. Happy programming! Please feel free in writing to me for further clarifications on other OOP concepts (im.himanshu2006@gmail.com) Himanshu

                          1 Reply Last reply
                          0
                          • S SPanicker

                            Hi All, Am just learning things. Can you explain me whether an Interface is a super set to Abstract Classes? In what way Abstract Classes can prove more useful than an interfaces? I know that it is not instantiable. Can you give me a small scenario were Abstract Class will be the only solution rather than using Interfaces?

                            Regards, Panicker.

                            L Offline
                            L Offline
                            LogiPro101
                            wrote on last edited by
                            #13

                            Difference between Abstract classes and Interface Little background: A real world entity is represented in an OOP world as object. Objects are defined as classes any language. A class defines objects in two aspects Object's characteristics - Data Members Object's functionality - Member functions So, when the classes that are forced to provide implementations of the functions prototypes that are defined in either ‘interfaces’ or a ‘abstract class’ are not belonging to the same family (having some inherited parental characteristics from same parent directly or indirectly) but are rather related through some functionalities then 100% we will go with interface and if above said classes are belonging to same family then we should go with abstract class inheritance. Also in case of inheritance as everything is a specialization of base parent object so there is allowance of providing some default definitions of the functions that are needed to be implemented further by specialized classes. This is not any way allowed in interfaces because implementing classes are not belonging to same family and are not any way related by characteristics. An Example: We have four real world objects: • Airplane • Bird • Sparrow • Eagle We have one method that is a functionality of all above objects • Fly Now all objects share common functionality flying in air but differ in the mechanism they use to fly. Also by close look we see that in general there is some common characteristics (as well as some common functionalities) between Sparrow and Eagle that can be a part of common class Bird (that has to be abstract because there is no real thing like bird!). But any way Airplane can't be related to Bird or its specialized classes Sparrow and Eagle. So if want to expose fly functionality to outer world and want their implementations in Airplane and Sparrow (or any bird), so I will 100% go for Interface and Abstract classes will be a big NO! But if we exclude Airplane want that fly functionality is exposed to outer world and its implementing classes are Bird, Sparrow and Eagle then 'Abstract Classes' is the best of best choice. Happy programming! Please feel free in writing to me for further clarifications on other OOP concepts (im.himanshu2006@gmail.com) Himanshu

                            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