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
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39836275
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
ID: 39836909
Particularly in what scenarios can I interfaces? Example please.
0
 

Author Comment

by:RajG1978
ID: 39836951
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
ID: 39837069
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 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