Solved

Can anyone give sample example where I can implement Interface and in what scenarios I should choose abstract class over interface?

Posted on 2014-02-05
4
297 Views
Last Modified: 2014-02-06
I know the differences between interfaces and abstract class(theoretical). I want to know particularly in .Net in what scenarios I should choose Interface and when I should use abstract sample. Please give me small example for both cases
0
Comment
Question by:RajG1978
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
An abstract class is a class that is incomplete and is use as a base for other complete classes. It does not have a constructor, but has methods that need to be overridden in the derived classes.

Think as a flowchart application such as Visio. You have many different shapes that have a set of common functions. You want to be able to drag them around, with a mechanism to align them on a grid, with connectors between them, and so on. You need these functions no matter is you move a rectangle, a circle or a lozenge. You could thus have an abstract class called Shape that defines and implement these functions.

However, you cannot draw the shape in that basic class, because it will be different for each different type of shape. You will thus declare the class as abstract, and declare in it an abstract method that will be used to Draw the shape. This method is simply a declaration, it has no code in it.

When you create a class for a new type of shape, a Lozenge for instance, you inherit from the abstract class in order to get all the basic functionality, but you will have to code the Draw method specific for a Lozenge. Each type of shape will have to provide it's own Draw method. The compiler won't let you go with a class that inherits from Shape and does not implement the Draw method.

---------

An interface is a little similar because it forces the classes that implements the interface to define a specific set of methods and properties. But contrary to an abstract class, an interface contains no code, no functionality, only a list of declarations of properties and methods that are needed for the interface to work.

A simple example of that in the framework is the IComparer interface. If you look carefully, most collections in the framework have a Sort method. But since most collections can accept different type of elements, how would it know that you what to sort a collection of Persons by their name, a collection or Orders by their order number, or any other collection first by a DateTime and then by an Integer.

Microsoft thus defined a IComparable interface, that defines the behavior of a CompareTo method that will define how the sort is to be performed. When you implement IComparable in a class, you are forced by the compiler to provide a method called CompareTo, that received an Object as a parameter, and returns an Integer. By following the rules set in the documentation for that method, you are defining the way to sort.

If you implement IComparable in a class, the compiler knows that you have defined the proper method and will enable Sort in collections of objects of that class.
0
 

Author Comment

by:RajG1978
Comment Utility
Particularly in what scenarios can I interfaces? Example please.
0
 

Author Comment

by:RajG1978
Comment Utility
Let me say I go for an interview and the interviewer asks me "Give me scenarios where you use Interface and where you would use Abstract class". What will be the answer.
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
You use both interfaces and abstract class to define a common set of properties and/or methods that different classes need to have so that you can handle them in a common way through polymorphism.

You use an abstract class when you can provide the implementation (code) of some of these methods and properties.

You use an interface when the methods and properties that you want to share all need to be defined (coded) inside of the classes that implement the interface.

Interfaces also have a second role. Since multiple inheritance (inheriting from 2 classes) is not permitted in .NET, an interface can be define to define a second set of common properties and methods that you want to share between classes that already inherits from another one, thus simulating multiple inheritance.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 35
.NET Error 7 42
Total in textboxes 9 35
Parallel.ForEach using SendMailAsync() detect when loop is complete 24 29
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now