Register
Sign In
Skip over navigation
Home
Add New Question
Jobs
Products
Abstract Class and Interface
Question posted
in
Computer Software
on
11 2009
Rate question difficulty level
4
2 Votes
What is the difference between Abstract class and Interface?
Add an answer
Share
|
6 Answers
Guest
Abstract classes allow for default default function definition. This means that whatever class extends the abstract class will have access to this. If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. An interface is a list of functions or properties that if a class implements it, it will have to have those functions defined within it. It is a situation of “Is-A” vs “Can-Do-this”. Objects that extends an Abstract class “Is-A” base class. Objects that implement “Can-Do-This”. Now if I asked this question and got the answer, yes, that would be the correct answer. However, I want to know why one would want to use an interface over an abstract class, and vice versa.
credits :
http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/
11/28/2009
Guest
If you have functionality that ALL derived classes should have, use an abstract class. If you want a "contract" that all classes will have certain features, use an interface.
12/04/2009
Guest
You can look at interface as another (partial) face of a class, a specific face that handles specific concerns of the class.
Abstract class is a base implementation of concerns that all derived classes share.
Since interfaces are polymorphic they provide essential tool to design a class with many concerns however without giving up the seperation of concerns within the same class.
The other way to achieve almost the same functionality would be to derive from more than one abstract class however this will cause two problems in your design:
1. what happens if two methods have the same name in your abstracts - which one will be the correct one to execute ?
2. deriving from a class which derives two classes will cause a join of concerns meaning that you no longer be able to distinguish between the functionality that each base class gave to the derived class.
To avoid this problems C# architects decided to provide interfaces and to constrain that a class can only derive from one class.
12/10/2009
Guest
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
abstract class MyBaseAbstract
{
public void M1()
{
int i = 10;
Console.WriteLine(i);
}
abstract public void M2();
}
class Program : MyBaseAbstract
{
public override void M2()
{
Console.WriteLine("I am OK");
}
static void Main(string[] args)
{
Program p = new Program();
p.M1();
p.M2();
}
}
}
interface
15
using System;
namespace ConsoleApplication2
{
public interface MyName
{
void Name();
}
class Program : MyName
{
#region MyName Members
public void Name()
{
Console.WriteLine("ahha");
}
#endregion
static void Main(string[] args)
{
Program p = new Program();
p.Name();
}
}
}
02/28/2010
Guest
Interface is a contract for the client i.e. user of a class. This class has implemented an interface.
Abstract class is a class which has defined as well as non-defined members(functions/properties etc).
The members which are non-defined should be preceded with 'abstract' keyword and so the class must be declared as abstract. Abstract class is a class that has no direct instances, but whose descendants may have direct instances.
Variables can be declared in abstract class, it is not allwed in interface. Interface contains only abstract members. Access modifiers are not allowed for interface members. Bydefault interface members are public virtual.
A class may inherit several interfaces but it can inherit only one abstract class.
Abstarct class can have constructor, interface cannot have constructor.
Abstract class is used as a root in inheritance hierarchy, whereas interface can be mixed up with hierarchy at any place.
02/28/2010
Guest
• Abstract class gives an Identity to the derived object.
• Interface extends the ability of an object.
• Interfaces give polymorphic behavior.
• Abstract class also gives polymorphic behavior. But it is more meaningful to say that Abstract class simplifies versioning, as the base class is flexible and inheriting class can create many versions of it by additional functions and also by implementing Interfaces.
• Interfaces and Abstract Classes both promote reusability.
02/28/2010
Add an answer*
Your name
Email
Enter the code shown above:
please wait..
Location:
United States
Tags:
c#
,
java
,
.net
,
Object oriented
Occupations:
Software\Web Development
,
Web\UI\UX Design
add a question
Now hiring!
IT & Engineering Professionals w/ Poly
SRA International
Herndon, VA
Business focuses on providing high-end information technology solutions and support to clients within the ... Architects ? Configuration Management Specialists ? Software Engineers ? Software Developers ? Geospatial...
---------------------------
IT Application Analyst
Saint Barnabas Health Care System
Oceanport, NJ
projects. Participate in and provide leadership for development and support of automation initiatives. Research ... skills. Experience in w orking with and developing hospital information systems. Patient Accounting and/or registration...
---------------------------
IT Mgr Software Engineering
Apollo Group
Phoenix, AZ
supervision and the software development guidelines and technology standards defined by Apollo Corporate IT, this ... Experience supervising and/or leading other professional level IT staff is required...
---------------------------
Senior Software Developer - Info Technology Job
Mercer
Deerfield, IL
Senior Software Developer - Info Technology-DEE000C5 Description Mercer is the global leader for trusted Human Resources ... information, please visit us at: www.mmc.com/diversity. Job Information Technology/Software Development Primary Location...
---------------------------
IT Development Manager
Farmers Insurance Group
Simi Valley, CA
Preferred Skills and Abilities Minimum 8+ years hands-on software development experience required; Minimum 4+ years ... required; Minimum 2+ years distributed (J2EE preferred) development experience required; Mainframe COBOL development...
---------------------------
Software Development Manager 2 - Health IT
Northrop Grumman
Providence, RI
Northrop Grumman is seeking an experienced Software Development Manager to join our team of qualified, diverse ... years of relevant experience - Experience with IT Project Development Lifecycle - Strong Medicaid/Medicare business...
---------------------------
IT Mgr Software Development
Apollo Group
Phoenix, AZ
supervision and the software development guidelines and technology standards defined by Apollo Corporate IT, this ... design, development, coding, testing and debugging of new software or significant enhancements to existing software.
---------------------------
Software Development Analyst 2 - Health IT
Northrop Grumman
Providence, RI
Northrop Grumman Information Systems Sector is seeking a Software Development Analyst 2 to join our team of qualified, ... development life cycle (SDLC) in support of application software from design, development, testing, documentation...
Jobs
by
Simply
Hired
View all jobs