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();
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.
//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.
//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");
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();
By running these two programs we can get clear idea on both abstract classes and interfaces.
|Author: Mrs. Meetu Choudhary Nanda 21 Feb 2009 Member Level: Gold Points : 2|
The class which contains the common features of components of several classes, but cannot it be instantiated by itself. It represents an abstract concept for which there is no actual existing expression. For instance, "Vegetation" is an abstract class - there is no such real, real thing as generic vegetation. Instead, there are only instances of vegetation, such as mango tree and rose plant, which are types of vegetation, and share common characteristics, such as having leaves and stem in at least part of the lifecycle.
SO in software engineering, an abstract class is a class in a nominative type system which is declared by the programmer, and which has the property that it contains members which are also members of some declared subtype. In many object oriented programming languages, abstract classes are known as abstract base classes, interfaces, traits, mixins, flavors, or roles. Note that these names refer to different language constructs which are (or may be) used to implement abstract types.
We can also say that abstract class is : -- A class which is used only as an ancestor and is never instantiated.
In other word a concrete definition will say that
A type of class with pure virtual member functions and one or more methods that are declared but not implemented, that behaves as a base class but prohibits the instantiation of any members of that class. i.e. It has a complete interface but only a partial implementation It is used to take advantage of inheritance yet prohibiting the generation of objects that are not completely defined. Concrete subclasses of an abstract class are required to flesh out the implementation by overriding the abstract methods.