You are on page 1of 2

C# Concepts



What is the need of abstract Classes. ?

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 Abstract class we can define COMMON functionalities in super class and those can be used in the derived class where as in Interface we cant do that. ( this i would say as advantage of abstract class) In case of Interface the derived class can implement any number of interface but Q: When should I use abstract methods? 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. Q: Why use an abstract class instead of an interface? A: The main reason to use an abstract class rather than an interface is because abstract classes can carry a functional “payload” that numerous subclasses can inherit and use directly. Interfaces can define the same abstract methods as an abstract class but cannot include any concrete methods. In a real program it is not a question of whether abstract classes or interfaces are better, because both have features that are useful. It is common to use a mixture of interface and abstract classes to create a flexible and efficient class hierarchy that introduces concrete methods in

concrete methods and combine them through the extends and implements keywords. engine and bodywork. . One specific case where abstract class may justifiably have no abstract methods is where it partially implements an interface. Chassis is not a viable implementation of a Vehicle in its own right. 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. To take a slightly contrived motoring analogy. Normally you should refactor to use a concrete superclass in this scenario. In practical terms it is more a question of the appropriate point in the hierarchy to define “empty” abstract methods.layers. so a concrete Car subclass would have to implement interface methods for functional wheels. Q: Why do we need abstract classes with no abstract methods? 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. with the intention that its subclasses must complete the interface.