Last post Nov 05, 2010 10:13 AM by atconway
Nov 01, 2010 01:22 PM|Skag55|LINK
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).
Nov 01, 2010 01:59 PM|XIII|LINK
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:
Nov 01, 2010 02:29 PM|logosoftv|LINK
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.
Sr. Software Engineer | Without us then who? |
919952070572 | My Blog
Nov 05, 2010 10:13 AM|atconway|LINK
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: