Solved

Interface and Abstract Class

Posted on 2013-05-16
1
236 Views
Last Modified: 2013-08-10
Hi,

If I am asked to make a choice between an abstract class and the interface, which one should i opt for. what are the things which i have to consider before choosing one among them.

Thanks in advance
Rajeeva
0
Comment
Question by:rajeeva_nagaraj
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39171131
the most substantial difference between abstract and interface is that interface is declarative only while abstract can contain implementation.
this is a good post to start with:
http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

in general, u choose abstract when u have shared functionality among your concrete objects.
for instance, let's say you have interface class Shape and concrete classes Triangle, Square etc, which derive from Shape base class.
now, and u want to add SaveToFile functionality.
instead of having the same implantation to all concrete classes, you could change the Shape class to abstract and implement SaveToFile there.
in relate to my previous example, when passing the shape objects as interface and not as abstract class, u can hide shared functionality which are internal.
for instance, the SaveToFile can be declared in the IShape interface.
assume u want to have common function to calculate area of shape but not to expose it in the interface.
the Shape abstract class is the place to add this function.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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