Answer to Question #3100 in C# for kevin peterkin
Class Shape defines function "int FindArea()" for example, this function is virtual and is implemented in the derived classes. Every specific class has the specific implementation of this method. The advantage of this type of design is that we can create the
collection of the Shape references and then add to this collection the objects of the derived classes. When we call the function FindArea(), it executes for the correct type of the object.
In C# we can do the same using the Interfaces.
Whe can also use such design to avoid the code duplication. For example we have two classes, DieselLocomotive and ElectricLocomotive. They have the identical functions (Drive(), Stop(), HitchWagons() and others) except the
function GetEnergy(). Instead of implementing two different classes with the almost identical functions we can create the abstract class Locomotive and implement all identical functions and the virtual function GetEnergy(). Then create two derived classes,& DieselLocomotive and ElectricLocomotive, extend& Locomotive and implement GetEnergy().
Need a fast expert's response?Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!