Introduction In almost all interviews everybody will face atleast one question which I mentioned in the following. 1)What is abstract class? 2)What is Interface? 3)Differences between Abstract class and Interfaces?
The article which I am giving will clearly explains you these three questions. Please read and compare all the comments which I mentioned in the program carefully. You can easily answer these three questions.
Abstract Class Program Here I am going to explain Abstract class program with simple example, In this program I commented some lines Please read one by one very carefully to get an idea of abstract calss. My explanation with simple program because everybody can get the basic idea of abstract class. abstract class aaa {
//In abstract class a method must be declared as abstract. //For abstract methods there will be no body i.e. There is no implementation. //The Abstract methods can delare with Access modifiers(public,internal,protected etc )But //when implementing in subclass same access modifier we have to use.
abstract public void pqr();
//The Abstract class can contain variables and concrete methods.
public int i; public void abc() { } }
//A class can Inherit only one Abstract class i.e Multiple inheritance is not possible here
class bbb : aaa {
//we have to must and should call all the abstract methods in the derived class. //To implement abstract method in derived class we have to use override keyword. //The accessmodifiers and return types(like void,int etc) must be same in abstract method and //in derived class method.
public override void pqr() { Console.WriteLine("Implementing abstract method"); } } public class zzz { public static void Main() {
//We cannot create an instance to the abstract classes i.e we cannot create an objects //for abstract classes //i.e aaa abs=new aaa();this will split an error.
bbb abs = new bbb(); abs.pqr(); } }
Interfaces Program Here I am going to explain Interfaces in similar way we can get clear idea. And compare the commentline which I mentioned in the same places for Abstract class and Interfaces we can easily understand what are the differences and similarities between abstract class and Interfaces.
interface yyy {
//For interface methods also there will be no body i.e. There is no implementation like abstract classes only. // In Interfaces we cannot use any access modifiers.By default these methods are public. // Note:-In interface all the methods are by default abstract methods only. //We cannot declare variables and concrete methods in interfaces.
void method1(); void method2(); } interface xxx { void method3(); }
//Here a class can inherit more than one class.Important:-Where we achieving multiple inheritance here.
class bbb : yyy, xxx {
//we have to must and should call all the interface methods in the derived class. //To implement interface methods in derived class no need of override keyword. //The accessmodifiers and return types(like void,int etc) must be same in interface methods and in derived class methods.
public void method1() { System.Console.WriteLine("in yyy class method1"); } public void method2() { System.Console.WriteLine("in yyy class method2"); } public void method3() { System.Console.WriteLine("in xxx class method3"); } } class mClass { public static void Main() {
//We cannot create an instance to the interfaces also like abstract classes i.e we cannot create an objects for Interfaces //i.e yyy abs=new yyy();this will split an compile error.
bbb abc = new bbb(); abc.method1(); abc.method2(); abc.method3(); } }
Summary By running these two programs we can get clear idea on both abstract classes and interfaces.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|