|
|
|
|
|
Home
»
.NET » .NET |
|
|
|
What’s the difference between an interface and abstract class?
|
|
|
|
Viewed :
62920
times
|
|
Major Category :
.NET
|
|
Minor Category :
ASP.NET
|
|
Total Replies :
27
|
|
|
|
|
|
|
|
|
EDITORIAL ANSWER |
|
|
No Reply Yet |
|
|
|
|
|
See if this Article Helps which in details covers difference bot programatically and theoratically.
http://www.c-sharpcorner.com/UploadFile/prasoonk/AbstractClassvsInterface06102009051117AM/AbstractClassvsInterface.aspx
|
|
satya kishore
Mar 24, 2009
|
|
|
|
for .net interview question and answers
pls visit my blog
satyakishore.webs.com
|
|
pula narasima
Jan 02, 2009
|
|
|
|
Hi,
Abstract class is the Act sa the base class,It supports implmented methods and unimplimented methods,Abstract calss as followes on derived class. in Abstract class you can Declare the Constructors,fields,methods,indexes,destructors etc Example: pulic Abstract Class A { public void Hi();
public void Hello() { Console.WriteLine("hello method"); }
} public Class B { public overid void Hi() { Console.WriteLine("hi method"); } } Interface: Interface is the Collection of Abstrcted method is called interface,By default is the Public in interface you can not Declare the Constructors,fields,methods,indexes,destructors . Examples:' //mutltiple inhiritance Interface IA { void cat(); void Dog(); } Interface IB { void door(); void tyres(); } public class C:IA,IB { public void cat() { Console.WriteLine("hello cat method"); } public void dog() { Console.WriteLine("hello dog method"); } public void door() { Console.WriteLine("hello door method"); } public void tyres() { Console.WriteLine("hello tyres method"); } }
|
|
Thaker Kandarp
Nov 08, 2008
|
|
|
|
Interface are similar to abstraction classes.However , interfaces represent the higest level of abstraction in Object- oriented programming.This is because all the methods in an interface are abstract and do not have implementation.In contrast ,the abstract classes might contain a method that has a body.
|
|
nitesh kumar
Aug 01, 2008
|
|
|
|
1).Interface have only signature. whereas Abstract class have signature and definition both r allow. 2). Interface have not allow modifier access. whereas Abstract class are allowed modifier access. 3).Thurogh the Interface we can create the Multiple Inheritance whereas Abstract class are not allow the Multiple Inheritance. 4).Interface is slower compare Abstract class.
|
|
nagaraju rekulapelli
Jul 13, 2008
|
|
|
|
Interface:-
1. Interfaces are used to declaring functionality.
2. By default all interface methods are public.
3. In class you can implement the interface method, but can’t implement the body in Interface method.
Abstract:-
1. Abstract will allow you to set the access specifier. Ex:- (private,public, protected, internal).
2. Abstract will allow you to implement the body in abstract methods.
3. You can inherit the abstract methods in classes.
|
|
|
|
|
|
in an interface class, all the methods are abstract, these is no implementation,and no accessbility modifiers are allowed, in an abstract class, some of the methods can be concrete,an abstract class may have some accessbility modifiers.
|
|
sanjeev singh
Jun 09, 2008
|
|
|
|
An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
· An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
· An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
· A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
· Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
|
|
sanjeev singh
Jun 09, 2008
|
|
|
|
An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
· An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
· An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
· A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
· Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
|
|
|
|
|
| | |
pervejkumar munjal
Mar 31, 2008
|
|
|
|
|
|
|
In a interface class, all methods are abstract without implementation where as in an abstract class some methods we can define concrete. In interface, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers. Interface and abstract class are basically a set of rules which u have to follow in case u r using them(inheriting them).
|
|
Elias Hossain
Feb 23, 2008
|
|
|
|
1. Abstract classes can have concrete method while interfaces have no methods implemented.
2. Interface do not come in inheriting chain, while abstract class come in inheritance.
3. Abstract classes are faster than interfaces.
4. By default, all members in Interface is Public whereas members in Abstract class may have different.
More .NET Questions and Answers
|
|
|
|
|
|
Interface only implementaion ,it is fully abstact classes.
Abstract is not implemented class,it is only inherited class
|
|
Shaik Hanumantha Rao
Jun 04, 2007
|
|
|
|
An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
· An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
· An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
· A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
· Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces. · Abstract classes are faster than interfaces.
|
|
|
|
|
|
interface just declaration.it provide standared for programming.it has no code.on the other hand abstaract class is one which can not make object.it is always inherited
such that ie
public abstarct class clscon
{
protected sqlconnection con= new sqlconnection()
public clscon()
{
con.connectionstring= configurationmanager.connectionstring("cn").connectionstring;
} }//this is abstract class
interface
public interface intemp
{
get;
set;
}
|
|
|
|
|
|
interface just declaration.it provide standared for programming.it has no code.on the other hand abstaract class is one which can not make object.it is always inherited
such that ie
public abstarct class clscon
{
protected sqlconnection con= new sqlconnection()
public clscon()
{
con.connectionstring= configurationmanager.connectionstring("cn").connectionstring;
} }//this is abstract class
interface
public interface intemp
{
get;
set;
}
|
|
|
|
|
|
There are basically three things to understand about interfaces and abstract classes:
1. Interfaces declare the methods a class must implement, but don't contain code.
2. Abstract classes may contain a mix of abstract and implemented methods. The abstract methods must be overridden in descendant classes.
|
|
Gurjinder Singh Brar
Jan 25, 2007
|
|
|
|
- An interface cannot provide any code; just the signature means all methods must be abstract. But an abstract class can provide complete, default code and/or just the details but at least at least one method must be abstract.
- A class may inherit several interfaces. But a class may inherit only one abstract class.
- If the various implementations only share method signatures then it is better to use Interface. If the various implementations are of the same kind and use common behavior or status then abstract class is better to use.
- If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. But if we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Thanks Gurjinder Singh Brar http://dotnet-question-answer.blogspot.com
|
|
|
|
|
|
in interfaces we can only define methods. But we cant implemented those methods in interfaces itself. interfaces allow us to have multiple inheritance.
in abstract classes we can define method and if we want we can implement those methods in abstact class. we cant instantiate from abstract classes.- by Shantha
|
|
| | |
|
|
|
|
in interfaces we can only define methods. But we cant implemented those methods in interfaces itself. interfaces allow us to have multiple inheritance.
in abstract classes we can define method and if we want we can implement those methods in abstact class. we cant instantiate from abstract classes.
|
|
|
|
|
|
What is an Interface?
An interface is a contract, a specification that concrete classes MUST follow. It defines method signatures but cannot have any implementations; the latter must be provided by the classes that implement the interface.
C# differs from C++ in this regard because C++ lacks native language support for interfaces. As a C++ programmers you have to create an interface by defining an abstract class with pure virtual methods.
what is an abstract class.................
An Abstract class lets you define some behaviors and force your subclasses to provide others. For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. So instead of trying to define these behaviors, the abstract class can declare abstract methods.
Differences between Interfaces and Abstract classes Which we use ?
I. multiple inheritance
A class may implement several interfaces but can only extend one abstract class.
II. default implementation
An interface cannot provide any code at all, much less default code. An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
III. adding functionality
If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.
IV. is-a vs -able or can-do
Interfaces are often used to describe the abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.
An abstract class defines the core identity of its descendants.
|
|
|
|
|
|
when u want to go for inheritance in so many derived classes but the methods implementations are different for different derived classes then u will make the class as abstract or interface. When all the method's implementations are based on derived class u will go for interface and if few implementations are common and remaining depends on derived class u will go for abstract class where u can have defined as well as declared methods also.
Note: abstract and interface classes are used when their methods are mandatory for derived classes and are not optional. They put restriction on derived classes that they cannot skip those methods withoud having them in it.
bye -Ram Nath Nishad
New Horizons India Ltd,
N-101,Manipal Center,
Bangalore,
India
|
|
|
|
|
|
There are lost of discussion on the internet about the Interface vs Abstract class. Also, as base class whether we have to use interface, abstract class or normal class.
I am trying to point out few considerations on which we can take decision about Interface vs Abstract class vs Class.
Abstract Class vs Interface
I am assuming you are having all the basic knowledge of abstract and interface keyword. I am just briefing the basics.
We can not make instance of Abstract Class as well as Interface.
Here are few differences in Abstract class and Interface as per the definition.
Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.
//Abstarct Class
public abstract class Vehicles { private int noOfWheel; private string color; public abstract string Engine { get; set; } public abstract void Accelerator(); }
//Interface public interface Vehicles { string Engine { get; set; } void Accelerator(); }
We can see abstract class contains private members also we can put some methods with implementation also. But in case of interface only methods and properties allowed.
We use abstract class and Interface for the base class in our application.
This is all about the language definition. Now million dollar question.
Thanks Manoj(InfoAxon Technology Limited)
|
|
|
|
|
|
In the interface all methods must be abstract, in the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
|
|
| | |
|
|
|
|
|
|
|
|
|