Resources » .NET programming » .NET Framework

Differences between Abstract class and Interfaces


Posted Date: 26-Jul-2007  Last Updated:   Category: .NET Framework    
Author: Member Level: Bronze    Points: 10


This article explains major differences between Abstract class and Interfaces with example program



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.


Did you like this resource? Share it with your friends and show your love!

Responses to "Differences between Abstract class and Interfaces"
Author: Miss Meetu Choudhary    21 Feb 2009Member Level: Gold   Points : 2
Abstract Class:



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.






Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Ram Prasad
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India