Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,
I am asp.net programmer.i have read the articles of abstract class and interface and i know how to implement it also.but can anybody explain me practically i.e. by referring project scenario where to use abstract class and where to use interface?

I need a very practical or real world project example, so that i can implement it in my project very efficiently?


Thanks in advanced.
Posted

Interface:

–> If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.

For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.

–> If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.

For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .

Abstract Classes

–> When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.

–> The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

For e.g. a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.

Use an abstract class

-When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
-Use an abstract class to define a common base class for a family of types.
-Use an abstract class to provide default behavior.
-Subclass only a base class in a hierarchy to which the class logically belongs.


Use an interface

-When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
-Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
-Use an interface to design a polymorphic hierarchy for value types.
-Use an interface when an immutable contract is really intended.
-A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

Also refer

When to use abstract classes instead of interfaces and extension methods in C#[^]
Abstract Class & Interface[^]
When should you use an abstract class, when an interface, when both?[^]

Hope it helps!
 
Share this answer
 
Comments
amitk_189 7-Oct-16 3:38am    
I was always in confusion regarding this, pretty informative.
Look at it this way:

An interface is like an abstract class in which everything is abstract. A 'normal' abstract class must not be purely abstract. There can be methods and properties that are implemented and accesses as in any other baseclasses. And don't forget that you can only inherit from a single class (abstract or not), but from as many interfaces as you want. An abstract baseclass already is out of the question if your derived class already must inherit from another class.
 
Share this answer
 
This is one of the answers I posted in response to a similar question
------------------------
Interface and abstract class both are used to serve two purpose

1. Having the facility for objects to work in polymorphic fashion.
2. Some kind of contract is enforced on the objects( the interface primarily defines the contract)

Now the decision of using which is based on

If you have only contract that should be enforced you should use interfaces.

But if you have some default behavior along with the contract that the derived classes should/could use then you should use abstract classes.

But I will not suggest going for either/or, My suggestion would be

1. Have an interface defining only the contract.
2. define an abstract class that implements this interface and then defines the default behavior functions.
3. Then have the concrete classes use the abstract classes. (they may choose to override some methods).

more like

Interface <------AbstactClass <---------------ConcreteClass

Gurus, please correct me if i said something wrong here.

2. and here is another one of similar nature
-------------------------------------------------------------------
Question asked already and here are the accepted answers:

My answer:
Well you are looking it the wrong way. It is not that you need to create interface always if you have to write classes. The interface are required when

1. You need multiple classes to behave in a polymorphic way.
2. You need some kind of contract to enforce on the classes. put the contract in interfaces.

and then a very awesome answer by SAKryukov

Let's start with the short answer by Rahul Rajat Singh. It's basically correct, but does not show the cases where only the interface could be used. Let me use this answer as the base and go from there.

In both cases, one could use not just the interface, but also a common base class; and that class could be purely abstract (by "purely" I mean that it might not have non-abstract members at all, in this case it is seemingly equivalent to interface). If so, why interfaces, why not always base classes?

Well, two things.

An interface could be implemented not only my class, but also by a structure, which is a value type, not a reference type. Polymorphism based on interfaces is more flexible then the one based on the base class. Polymorphous containers become agnostic not only to the run-time type of the elements but even to the nature of implementing type: it can be either value or reference type. It gives more flexibility.

More importantly, interfaces and interface implementing types are way more flexible in inheritance: even though a class can have only one immediate base class, a class or a structure can implement more then one interface; and each interface can have more than one interface in inheritance list. This is called weak form of multiple inheritance. In practice, this makes it possible to develop some variants of usage impossible with classes. For example: 1) a class based on some class irrelevant to some polymorphous set can still become a member of the set as we can derive a class implementing required interface; 2) we can make objects participating in two or more different polymorphous sets; to do so, it's enough to implement two or more different interfaces by the same class or structure.

-SA
 
Share this answer
 
In a practical Approach interface is used to make a template frame which we can use in more then one class.
for example :

C#
public interface class Ia
{
int area();
}


for implementation of Interface Ia we can implement a area() for calculating area of square and area of rectangle and we can get the data for area by just creating a reference of class which is as shown below:
C#
public class Squarecls:Ia
{
public int area()
{
// since Area of Square A=a*a , therefore
int a=5;
return a*a;
}
}


C#
public class Rectangle_cls:Ia
{

public int area()
{
// since Area of Rectangle A=l*w , therefore
int L=5;
int W=9
return L*W;
}


}


C#
public static void program()
{

// getting Area of Rectangle
Ia ref=new Rectangle_cls()
int getAreaRectangle=ref.Area();

// getting Area of Square 
 Ia refsquare=new Squarecls()
int getAreaSquare=refsquare.Area();
}
 
Share this answer
 
Comments
CHill60 9-Oct-15 6:33am    
Post was answered over 3 years ago. You haven't added anything to the solutions already posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900