locked
What is the point of defining a method abstract? RRS feed

  • Question

  • User-1494545087 posted

    Hi,

    I am reading a C# book and it says that in order to force classes to override a method (say Draw() ) you define it as abstract.

    So far so good. However what's the point of forcing each class to override a method implementation, and not just create a new method for each class?

    An example is the one with the Shapes class and the Draw() method. Each shape class must override the public abstract void Draw() method in order to draw itself.

    Why not ommit that, and define a Draw method inside each shape? (i.e. square class, triangle class etc).

    Thanks

    Monday, November 1, 2010 1:22 PM

Answers

  • User541108374 posted

    Hi,

    with this approach one can pass in a shape, no matter if it's a rectangle/triangle/circle/..., into a method for example. Since the Draw method's known on the base type it can be called via the base type but the implementation of the instance of the subclass will be called.

    A nice video, it makes use of interfaces instead of abstract classes but shows the same idea, is this one: http://tekpub.com/view/concepts/4.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 1:59 PM
  • User-1780871514 posted

    In C# interfaces all the classes are abstract by default. So the implementing classes must override all the methods defined in Interface.

    This will helpfull when we need to force the developers to follow some certain architecture in their codes, We can create interfaces and force them to implement in their code development.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 2:29 PM
  • User-952121411 posted

    However what's the point of forcing each class to override a method implementation, and not just create a new method for each class?
     

    Take a look at this response I just typed up about a week ago on Abstract Classes and their usefulness to see if it helps answer your question:

    Abstract Classes:

    http://forums.asp.net/p/1616187/4138458.aspx#4141431

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 5, 2010 10:13 AM

All replies

  • User541108374 posted

    Hi,

    with this approach one can pass in a shape, no matter if it's a rectangle/triangle/circle/..., into a method for example. Since the Draw method's known on the base type it can be called via the base type but the implementation of the instance of the subclass will be called.

    A nice video, it makes use of interfaces instead of abstract classes but shows the same idea, is this one: http://tekpub.com/view/concepts/4.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 1:59 PM
  • User-1780871514 posted

    In C# interfaces all the classes are abstract by default. So the implementing classes must override all the methods defined in Interface.

    This will helpfull when we need to force the developers to follow some certain architecture in their codes, We can create interfaces and force them to implement in their code development.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 2:29 PM
  • User-952121411 posted

    However what's the point of forcing each class to override a method implementation, and not just create a new method for each class?
     

    Take a look at this response I just typed up about a week ago on Abstract Classes and their usefulness to see if it helps answer your question:

    Abstract Classes:

    http://forums.asp.net/p/1616187/4138458.aspx#4141431

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 5, 2010 10:13 AM