Você está na página 1de 4

Abstract Class Abstract class is a class which cannot be instantiate.

Abstract class may have both concrete and abstract members, i.e we may have the implementation and we may not have the implementation in case of abstract members. Multiple inheritances are not supported in case of Abstract class. It is a kind of contract that enforces all the derived classes to carry on the same hierarchies or standards A class does not implement the multiple abstract classes Abstract class members can have access modifiers A class can inherit from multiple interfaces at the same time an abstract class can have fields. an abstract class can inherit from another abstract class or another interface. Abstract classes can have implementations for some of its members It allow us to specify all access modifier except Private Abstract class may contain abstract methods and accessors. Abstract modifier can be used with classes, methods and properties. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors. Abstract method declarations are only permitted in abstract classes. Abstract class cannot be instantiated. Abstract class methods may OR may not have an implementation
An Abstract class doesn't provide full abstraction

Interface Interface is a template which cannot be instantiate. Interface should have only abstract members and without implementation.

Multiple inheritances are supported in case of Interfaces. To implement the same method in the all derived classes A Class can call the multiple Interfaces. interface members cannot have access modifiers. a class cannot inherit from multiple classes at the same time. Interfaces cannot have fields An interface can inherit from another interface only and cannot inherit from an abstract class nterface can't have implementation for any of its members. It allows only public Access modifier

They can have member properties and methods. All the properties and methods of interfaces are abstract. They have no body, just the declaration.

Public, Protected, Private, Friend, Shared, Overrides, MustOverride, NotOverridable are permitted inside an interface. Interfaces are similar to classes.
methods in an Interface only have a definition, no implementation. an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface. using an Interface we can achieve multiple inheritance. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation. A class can implement any number of interfaces

Using Abstract we can not achieve multiple inheritance

An abstract class can contain fields, constructors, or destructors and implement properties. a subclass can at most use only one abstract class.

Abstract class can implement more than one interfaces, but can inherit only one class

Interface can inherit more than one interfaces

Interfaces and Abstract classes *An Interface cannot implement methods. *An abstract class can implement methods. *An Interface can only inherit from another Interface. *An abstract class can inherit from a class and one or more interfaces. *An Interface cannot contain fields. *An abstract class can contain fields. *An Interface can contain property definitions. *An abstract class can implement a property. *An Interface cannot contain constructors or destructors. *An abstract class can contain constructors or destructors. *An Interface can be inherited from by structures. *An abstract class cannot be inherited from by structures. *An Interface can support multiple inheritance. *An abstract class cannot support multiple inheritance.

Abstract Class 1.It can contain Concrete methods(methods with implementation) So in other words, Abstract class can contain methods with both implemetaion and without implementation 2.Multiple inheritance is not possible in case of abstract class 3.Access Specifiers are been Supported in abstract class

Interface 1.Does not contain any concrete methods 2.Multiple Inheritance is possible with interface 3.Access Specifiers are not supported in Interface

basically 5 differences between Abstract Class and Interface which are listed as below :1. A class can implement any number of interfaces but a subclass can at most use only one abstract class.

2. An abstract class can have non-abstract Methods(concrete methods) while in case of Interface all the methods has to be abstract. 3. An abstract class can declare or use any variables while an interface is not allowed to do so. 4. An abstract class can have constructor declaration while an interface can not do so. 5. An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface we can not declare any access modifier(including public) as all the members of interface are implicitly public.

Interface in C#:

An Interface is not a class, we can say it an entity or a template that is define by the word Interface. We cannot instantiate the Interface and it is just like implementing the polymorphism. An interface should not contain any code in it only method signature should be there. Abstract Class in C#:

Abstract class is just like as normal class but which cannot be instantiated. It means we cannot create an object for the Abstract class. An abstract class starts with the Keyword Abstract. It contains concrete and abstract members. A concrete method may have the implementation but abstract member should only have the signature.

Whats the difference between an interface and abstract class? 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.

What are the other characteristics of an abstract class: 1. 2. 3. 4. A class may inherit only from only one abstract class Cannot be instantiated directly An abstract class may contain abstract methods and accessors. Methods marked as abstract cannot contain any code.

5. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors otherwise the Compiler Error CS0534 will occur. 6. An abstract method is implicitly a virtual method. 7. Abstract properties behave like abstract methods,

Some of the characteristics of an Interface: 1. 2. 3. 4. 5. An interface cannot be instantiated directly. A class or struct can implement more than one interface. Interfaces can contain events, indexers, methods, and properties. An interface itself can inherit from multiple interfaces. The interface itself contains no implementations but only the public members signature