1)
Read more ...
Interfaces provide a form of multiple inheritance. A class
can extend only one other class.
Interfaces are limited to public methods and constants with
no implementation. Abstract classes can have a partial
implementation, protected parts, static methods, etc.
A Class may implement several interfaces. But in case of
abstract class, a class may extend only one abstract class.
Interfaces are slow as it requires extra indirection to to
find corresponding method in in the actual class. Abstract
classes are fast.
2)
(1) 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.
(2) An abstract class can contain fields,
constructors, or destructors and implement properties. An
interface can not contain fields, constructors, or
destructor and it has only the property's signature but no
implementation.
(3) 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.
(4) 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.
(5) Various access modifiers such as abstract,
protected, internal, public, virtual, etc. are useful in
abstract Classes but not in interfaces.
(6) Abstract classes are faster than interfaces.
3)
Interface :
1.Interface have only Signature.
2.All the Methods are Public , It doesn't have access Modifier Controls
3.It have used Multiple inheritence in the Object oriented Language
4.All the Methods are Abstract.
5.It does not have Contructor,destructor,Fileds
6.A Class May inherits several Interfaces
Abstract Class:
1.Abstract Class have Method defination and Implementation
2.It have control the Access Modifiers
3.It does not allow multiple Inheritence
4.Some methods are concrete
5. It have Contructor and destructor
6.Only one abstract have to derived
4)
class,
we make use of the abstractkeyword. Such a class cannot be
instantiated. Syntax below: abstract public class Vehicle { } Above,
an abstract class named Vehicle has been defined. We may use the fields,
properties and member functions defined within this abstract class to create
child classes like Car,Truck, Bike etc. that inherit the features defined
within the abstract class. To prevent directly creating an instance of the
class Vehicle, we make use of the abstract keyword. To use the definitions
defined in the abstract class, the child class inherits from the abstract
class, and then instances of the Child class may be easily created.
Further, we may define abstract methods within an abstract class (analogous to
C++ pure virtual functions) when we wish to define a method that does not have
any default implementation. Its then in the hands of the descendant class to
provide the details of the method. There may be any number of abstract methods
in an abstract class. We define an abstract method using the abstract keyword.
If we do not use the abstract keyword, and use the virtual keyword
instead, we may provide an implementation of the method that can be used by the
child class, but this is not an abstract method.
Remember, abstract class can have an abstract method, that does not have any
implementation, for which we use the abstract keyword, OR the abstract class
may have a virtual method, that can have an implementation, and can be
overriden in the child class as well, using the override keyword.
Read example below Example: Abstract Class with Abstract method
namespace Automobiles
{
public abstract class Vehicle
{
public abstract void Speed() //No
Implementation here, only definition
}
}
Example: Abstract Class with Virtual method
namespace Automobiles
{
public abstract class Vehicle
{
public virtual void Speed() //Can
have an implementation, that may be overriden in child class
{
...
}
}
Public class Car : Vehicle
{
Public override void Speed()
//Here, we override whatever implementation is there in the abstract class
{
... //Child
class implementation of the method Speed()
}
}
} An Interface is
a collection of semantically related abstract members. An interface expresses
through the members it defines, the behaviors that a class needs to support. An
interface is defined using the keyword interface. The members
defined in an interface contain only definition, no implementation. The members
of an interface are all public by default, any other access specifier cannot be
used. See code below: Public interface IVehicle //As a convention,
an interface is prefixed by letter I
{
Boolean HasFourWheels()
} Time
to discuss the Difference between Abstract Class and Interface
1) A class may inherit only one abstract class, but may implement
multiple number of Interfaces. Say a class named Car needs to inherit some
basic features of a vehicle, it may inherit from an Aabstract class named
Vehicle. A car may be of any kind, it may be a vintagecar, a sedan, a coupe, or a racing car. For these
kind of requirements, say a car needs to have only two seats (means it is a
coupe), then the class Car needs to implement a member field from an interface,
that we make, say ICoupe.
2) Members of an abstract class may have any access modifier, but
members of an interface are public by default, and cant have any other access
modifier.
3) Abstract class methods may OR may not have an implementation,
while methods in an Interface only have a definition, no implementation.