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. Objects, Abstract Class or Interface?

Objects, Abstract Class or Interface?

Scheduled Pinned Locked Moved C#
csharptutorialquestiondiscussion
7 Posts 6 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.
  • W Offline
    W Offline
    Wayne Phipps
    wrote on last edited by
    #1

    I'm trying to get my head around objects and am asking for thoughts on this idea. If for example I were to implement a Person Object, there may be differnt Person objects representing people of different cultures. All Person objects have similar characteristics, ie Height, Weight, Age ect but I imagine that there could be a method ie SayHello which Person objects of different cultures would implement differently. Now what i'm trying to figure out is whether the Person Class should be abstract to cope with different cultures or whether I should create an interface or both. Thoughts and comments appreciated. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

    N A A 3 Replies Last reply
    0
    • W Wayne Phipps

      I'm trying to get my head around objects and am asking for thoughts on this idea. If for example I were to implement a Person Object, there may be differnt Person objects representing people of different cultures. All Person objects have similar characteristics, ie Height, Weight, Age ect but I imagine that there could be a method ie SayHello which Person objects of different cultures would implement differently. Now what i'm trying to figure out is whether the Person Class should be abstract to cope with different cultures or whether I should create an interface or both. Thoughts and comments appreciated. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Although you could implement it with interfaces, a simple virtual method for SayHello, or other methods, would be simplier.

      1 Reply Last reply
      0
      • W Wayne Phipps

        I'm trying to get my head around objects and am asking for thoughts on this idea. If for example I were to implement a Person Object, there may be differnt Person objects representing people of different cultures. All Person objects have similar characteristics, ie Height, Weight, Age ect but I imagine that there could be a method ie SayHello which Person objects of different cultures would implement differently. Now what i'm trying to figure out is whether the Person Class should be abstract to cope with different cultures or whether I should create an interface or both. Thoughts and comments appreciated. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

        A Offline
        A Offline
        Andrew Kirillov
        wrote on last edited by
        #3

        Hello I don't think there are any requirements for using interface in your case. You can create your base Person class, with a virtual SayHello method. You can provide it with default realization (may the person can greet you on some kind of default language of human being saying "Mmmm" :-D or with some other action). Then, any other class, which will inherit your base class, can override the method. Or you can make the method abstract to make it obligate to provide realization of the method. With best regards, Andrew

        W J 2 Replies Last reply
        0
        • W Wayne Phipps

          I'm trying to get my head around objects and am asking for thoughts on this idea. If for example I were to implement a Person Object, there may be differnt Person objects representing people of different cultures. All Person objects have similar characteristics, ie Height, Weight, Age ect but I imagine that there could be a method ie SayHello which Person objects of different cultures would implement differently. Now what i'm trying to figure out is whether the Person Class should be abstract to cope with different cultures or whether I should create an interface or both. Thoughts and comments appreciated. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          the rule of thumb is: class inheritence is used as "is a" ... "an american IS A person" while interfaces are more used in a context of "can perform something" "money is cloneable" (where you might have ICloneable) //Roger

          1 Reply Last reply
          0
          • A Andrew Kirillov

            Hello I don't think there are any requirements for using interface in your case. You can create your base Person class, with a virtual SayHello method. You can provide it with default realization (may the person can greet you on some kind of default language of human being saying "Mmmm" :-D or with some other action). Then, any other class, which will inherit your base class, can override the method. Or you can make the method abstract to make it obligate to provide realization of the method. With best regards, Andrew

            W Offline
            W Offline
            Wayne Phipps
            wrote on last edited by
            #5

            Why make the SayHello method virtual? Could you noy just Override the method in the derrived class? I understand that making it abstract forces the derrived class to implement the method in its own way but again, could this not also be achieved by Overriding the inherited method? Is there a benefit or forefeit to either approach? Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

            G 1 Reply Last reply
            0
            • W Wayne Phipps

              Why make the SayHello method virtual? Could you noy just Override the method in the derrived class? I understand that making it abstract forces the derrived class to implement the method in its own way but again, could this not also be achieved by Overriding the inherited method? Is there a benefit or forefeit to either approach? Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

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

              You can only override a virtual method. That is the reason for making it virtual. --- b { font-weight: normal; }

              1 Reply Last reply
              0
              • A Andrew Kirillov

                Hello I don't think there are any requirements for using interface in your case. You can create your base Person class, with a virtual SayHello method. You can provide it with default realization (may the person can greet you on some kind of default language of human being saying "Mmmm" :-D or with some other action). Then, any other class, which will inherit your base class, can override the method. Or you can make the method abstract to make it obligate to provide realization of the method. With best regards, Andrew

                J Offline
                J Offline
                Jaiprakash M Bankolli
                wrote on last edited by
                #7

                I agree with Andrew, I think that would be the best way to solve this requirement. You can override the method of sayhello of base class and implement it in the child class Regards, Jaiprakash M Bankolli

                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