7
Reply

Abstract class and an Interface.

Manoranjan Gupta

Manoranjan Gupta

10y
2.4k
0
Reply

    Class can extend only one abstract class and class can implement numerous interface.

    Probably "Difference Between abstract Class and Interface" is the most frequent question being asked in .Net world . In this tutorial, I will explain the difference theoretically followed by code snippet. Theoretically there are basically 5 differences between Abstract Class and Interface which are listed as below :-A class can implement any number of interfaces but a subclass can at most use only one abstract class. An abstract class can have non-abstract Methods(concrete methods) while in case of Interface all the methods has to be abstract. An abstract class can declare or use any variables while an interface is not allowed to do so.So following Code will not compile :-interface TestInterface {int x = 4; // Filed Declaration in Interfacevoid getMethod();string getName(); }abstract class TestAbstractClass {int i = 4;int k = 3;public abstract void getClassName(); }It will generate a compile time error as :-Error 1 Interfaces cannot contain fields .So we need to omit Field Declaration in order to compile the code properly.interface TestInterface {void getMethod();string getName(); } abstract class TestAbstractClass {int i = 4;int k = 3;public abstract void getClassName(); }Above code compiles properly as no field declaration is there in Interface.An abstract class can have constructor declaration while an interface can not do so.So following code will not compile :-interface TestInterface {// Constructor Declarationpublic TestInterface(){}void getMethod();string getName(); }abstract class TestAbstractClass {public TestAbstractClass(){}int i = 4;int k = 3;public abstract void getClassName(); }Above code will generate a compile time error as :-Error 1 Interfaces cannot contain constructors So we need to omit constructor declaration from interface in order to compile our code .Following code compile s perfectly :-interface TestInterface {void getMethod();string getName(); }abstract class TestAbstractClass {public TestAbstractClass(){}int i = 4;int k = 3;public abstract void getClassName(); }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. Note here I am talking about the access specifiers of the member of interface and not about the interface.Following code will explain it better :-It is perfectly legal to give provide access specifier as Public (Remember only public is allowed)public interface TestInterface {void getMethod();string getName(); }Above code compiles perfectly.It is not allowed to give any access specifier to the members of the Interface.interface TestInterface {public void getMethod();public string getName(); }Above code will generate a compile time error as :-Error 1 The modifier 'public' is not valid for this item.But the best way of declaring Interface will be to avoid access specifier on interface as well as members of interface.interface Test {void getMethod();string getName(); }

    Abstrac class is used to write static method and use it in through out application.Interface is used to declare proto type only

    Example on Interface and Abstract interface common { void sms(); void calling(); } interface asb { } abstract class features { public void internetaccess() { } //declare abstract methods here } public class Nokia2690 : features, common,asb { //common methods should have body here //if it is abstract methods should be definition here //if it is concrete methods may or may not be //No need to create object for abstract and interfaces u can take number of interfaces }

    o Interface definition begins with a keyword interface so it is of type interface o Abstract classes are declared with the abstract keyword so it is of type class o Interface has no implementation, but they have to be implemented. o Abstract class's methods can have implementations and they have to be extended. o Interfaces can only have method declaration (implicitly public and abstract) and fields (implicitly public static) o Abstract class's methods can't have implementation only when declared abstract. o Interface can inherit more than one interfaces o Abstract class can implement more than one interfaces, but can inherit only one class o Abstract class must override all abstract method and may override virtual methods o Interface can be used when the implementation is changing o Abstract class can be used to provide some default behavior for a base class. o Interface makes implementation interchangeable o Interface increase security by hiding the implementation o Abstract class can be used when implementing framework o Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies. This are some more points Regarding difference between Interface and Abstract classes For learning more about c#interview questions you can refer following Link csharp interview questions

    Abstract Class V/s Interface (1) :-Abstract class can have implementation for some of its method. But the interface can't have implementation for any of its method.(2) :-Abstract class can have field but interface does not have fields. (3) :-An Interface can inherit from another interface only can't inherit from another abstract class but an abstract class can inherit from another abstract class and another interface. (4) :-Abstract class members can have access modifiers where interface member doesn't have access modifiers.You can go on this link for more difference.. http://dotnet-munesh.blogspot.in/2013/12/difference.html

    1> class can extend only one abstract class and class can implement numerous interface.

    2>The members of abstract class can be private as well as protected but Interface have public members.

    3>Any class can extend an abstract class and interface can extend another interface

    4>Methods in abstract class can be abstract as well as concrete and All methods in an interface should be abstract

    5There can be a constructor for abstract class but Interface does not have constructor.

    6>The class extending the abstract class may or may not implement any of its method but its not happen in case of interface(contract between class and interface to implement ) 

    7>An abstract class can implement methods but Interfaces cannot contain body of any of its method.

    8>Abstract classes should have sub classes and Interface implemented by class(contract between class and interface)