Concepts of C# - Need of Abstract
Concepts of C# - Need of Abstract
We use Abstract class and interface to enforce some rules to the classes
which extends/implements. For example we can define a class say "Bird" and
we can declare methods say "Fly()", "Walk()". This means whatever the class
that is derived from Bird has to override or give definition for Fly() and Walk()
and therefore we are making sure that all the derived classes follows or has
the common functionalities. In other way the classes derived from superclass
should have common properties. In addition to this the derive class can have
its own methods like "Swim()"...
In case of Interface the derived class can implement any number of interface
but
A: Abstract methods are usually declared where two or more subclasses are
expected to fulfil a similar role in different ways. Often the subclasses are
required to the fulfil an interface, so the abstract superclass might provide
several of the interface methods, but leave the subclasses to implement
their own variations of the abstract methods. Abstract classes can be
thought of as part-complete templates that make it easier to write a series of
subclasses.
A: An abstract class without any abstract methods should be a rare thing and
you should always question your application design if this case arises.
Normally you should refactor to use a concrete superclass in this scenario.
One specific case where abstract class may justifiably have no abstract
methods is where it partially implements an interface, with the intention that
its subclasses must complete the interface. To take a slightly contrived
motoring analogy, a Chassis class may partially implement a Vehicle interface
and provide a set of core methods from which a range of concrete Vehicle
types are extended. Chassis is not a viable implementation of a Vehicle in its
own right, so a concrete Car subclass would have to implement interface
methods for functional wheels, engine and bodywork.