What’s the difference between an interface and abstract class?
nikhil kansal
Select an image from your device to upload
Interface only implementaion ,it is fully abstact classes.
Abstract is not implemented class,it is only inherited class
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.
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.
public abstarct class clscon{ protected sqlconnection con= new sqlconnection() public clscon() { con.connectionstring=configurationmanager. connectionstring("cn").connectionstring; }}//this is abstract class
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;}
public interface intemp
get;
set;
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 ?
multiple inheritanceA class may implement several interfaces but can only extend one abstract class.
default implementationAn 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.
adding functionalityIf 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.
is-a vs -able or can-doInterfaces 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.
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.