Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » Abstraction and Interface in C#

Abstraction and Interface in C#

Here I will explain the abstraction and interface in c# by using simple example.

Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 329
Total page views :  50350
Rating :
 1/5
This article has been rated :  7 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
Abstraction.zip
 
Become a Sponsor



Abstraction

using System;
namespace OOPs
{
    
/// <summary>
    
/// Summary description for Abstract1.
    
/// </summary>
    
/// Abstract class
    
abstract class Abstract1
    
{
         
public Abstract1()
          {
              
// TODO: Add constructor logic here
         
}
          
//non-abstract method
          
public void nonabmethod()
     {
          System.
Console.WriteLine("Non Abstract Method!");
     }
         
//abstract method
         
public abstract void abmethod(); //they are implicitly virtual it is not contain the body of the method
         
public static void Main()
          {
              
myclass mycls = new myclass();
               mycls.nonabmethod();
               mycls.abmethod();
          }
     }
         
class myclass : Abstract1 //class derived from abstract class
         
{
              
public override void abmethod() //must implement the abstract method derived in class
         
{
               System.
Console.WriteLine("Abstract Method!");
          }
     }

}

using System;
namespace OOPs
{
    
/// <summary>
    
/// Summary description for Abstract2. Partial implementation of abstract class
    
/// </summary>
    
abstract class Abstract2
    
{
         
public static void Main()
          {
              
myclass2 mycl = new myclass2();
               mycl.method1();
               mycl.method2();
          }
         
public Abstract2()
          {
              
// TODO: Add constructor logic here
          
}
          
public abstract void method1();
          
public abstract void method2();
          }
          
abstract class myclass1 : Abstract2
         
{
              
public override void method1()
               {
                   
Console.WriteLine("Abstract Method #1");
               }
          }
           
class myclass2 : myclass1
         
{
          
public override void method2()
          {
              
Console.WriteLine("Abstract Method #2");
          }
     }
}

using System;
namespace OOPs
{
    
/// <summary>
    
/// Summary description for Abstract3.
    
/// </summary>
    
/// abstract class derived from non abstract class
    
public class Abstract3
    
{
         
public static void Main()
     {
         
mainclass mcl = new mainclass();
          mcl.noabsmethod();
          mcl.absmethod();
     }
     
public Abstract3()
     {
         
// TODO: Add constructor logic here
     
}
     
public void noabsmethod()
     {
         
Console.WriteLine("Non-Abs class method");
     }
}
abstract class absclass : Abstract3
{
    
public abstract void absmethod();
}
class mainclass : absclass
{
    
public override void absmethod()
{
    
Console.WriteLine("Abstract method call");
}
}
}

using System;
namespace OOPs
{
    
/// <summary>
    
/// Summary description for Abstract4.
    
/// </summary>
    
/// abstract class dervied from interface
    
public abstract class Abstract4 : myowninterface
    
{
         
public static void Main()
     {
         
myclasses mc = new myclasses();
          mc.IMethod();
     }
         
public Abstract4()
          {
              
// TODO: Add constructor logic here
          
}
          
public void IMethod()
          {
              
Console.WriteLine("Method implementation from interface");
          }
}
interface myowninterface
{
    
void IMethod();
}
class myclasses : Abstract4
{
}
}

using System;
namespace OOPs
{
    
/// <summary>
     
/// Summary description for Abstract5.
    
/// </summary>
    
public abstract class Abstract5
    
{
         
public static void Main()
          {
              
Abstract5 abs = new baseclass(); // Polymorphism
              
abs.method();
               abs.method1();
          }
      
public Abstract5()
     {
          
// TODO: Add constructor logic here
     
}
        
public abstract void method();
        
public abstract void method1();
}
class baseclass : Abstract5
{
     public override void method()
{
     Console.WriteLine("Abstract Method1 of baseclass");
}
    
public override void method1()
{
    
Console.WriteLine("Abstract Method2 of baseclass");
}
}
}

Interface:

using System;
namespace OOPs
{
    
/// <summary>
    
/// Summary description for Interface1.
    
/// </summary>
    
public class Interface1 : myinterface
     {
         
public static void Main()
     {
         
Interface1 inf1 = new Interface1();
          inf1.callmethod();
     }
     
public Interface1()
     {
         
// TODO: Add constructor logic here
     
}
      
public void callmethod()
     {
         
Console.WriteLine("callmethod implemented!");
     }

}
interface myinterface
{
void callmethod();
}
}

using System;
namespace OOPs
{
/// <summary>
/// Combining Interfaces
/// </summary>
public class Interface2 : combineinterface
{
    
public static void Main()
{
    
Interface2 inf2 = new Interface2();
     inf2.show();
     inf2.display();
}
public Interface2()
{
    
// TODO: Add constructor logic here
}
public void show()
{
    
Console.WriteLine("Show Method Call!");
}
    
public void display()
{
    
Console.WriteLine("Dislay Method Call!");
}
}
interface myinterface1
{
void show();
}
interface myinterface2
{
void display();
}
interface combineinterface : myinterface1, myinterface2
{
}
}

using
System;
namespace OOPs
{
    
public class Interface3 : interdemo
     {
         
public static void Main()
          {
              
Interface3 inf3 = new Interface3();
               inf3.show();
              
if (inf3 is interdemol)
               {
                   
interdemol id = (interdemol)inf3;
                   
bool ok = id.display();
                   
Console.WriteLine("Method implmented");
               }
              
else
              
{
                   
Console.WriteLine("Method not implmented");
               }
          }
          
public Interface3()
          {
              
// TODO: Add constructor logic here
          
}
          
public bool show()
          {
              
Console.WriteLine("Show method call");
              
return true;
          }
     }
interface interdemo
{
    
bool show();
}
interface interdemol
{
    
bool display();
}
}

using
System;
namespace OOPs
{
    
public class Interface4 : inf1, inf2
{
    
public static void Main()
{
    
Interface4 infobj = new Interface4();
}
    
public Interface4()
{
    
// TODO: Add constructor logic here
}
void inf1.show12()
{
    
Console.WriteLine("call show method1!");
}
void inf2.show12()
{
    
Console.WriteLine("call show method2!");
}
}
interface inf1
{
    
void show12();
}
interface inf2
{
    
void show12();
}
interface inf : inf1, inf2
{
}
}


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Paresh Vaishnav
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
Abstraction.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Dundas Dashboard
Become a Sponsor
 Comments
Need more details by Mahesh On December 12, 2007
I think you should write more detais about the code in this article on how abstraction and interfaces work in your sample.
Reply | Email | Delete | Modify | 
Useless by Petar On January 4, 2008
Just the code like this is ABSOLUTELY useless ! Think about real names and usages, things like myInterface and myAbstractClass are plain stupid. Please don't write another article before you read about 50 other articles writed by someone else.
Reply | Email | Delete | Modify | 
Wow - what a great article - NOT! by Gary On January 11, 2008
How about some explaination!
Reply | Email | Delete | Modify | 
difference by ronak On January 17, 2008
plz suggest me what is the difference between abstract class & interface
Reply | Email | Delete | Modify | 
get a life man by Ganesh On January 31, 2008
what's wrong with you? do you think coding is just "copy and paste"? Go through msdn and other great blogs and learn how to write an article
Reply | Email | Delete | Modify | 
interface related questions by Faisal On February 6, 2008
what is default type of interface ? i know its members are public but what about it. why the interface cannot be virtual or static ?
Reply | Email | Delete | Modify | 
Worthless by Jason On March 15, 2008
Yeah, this was a waste of time.
Reply | Email | Delete | Modify | 
ABOUT ABSTRACTION & INTERFACE by jadav On December 26, 2008
ITS NOT THAT MUCH CLEAR PARESH VAISHNAV TRY TO EXPLAIN CLEARLY & BRIEFLY
Reply | Email | Delete | Modify | 
unhelpful by khemakhem On January 28, 2009
where is comment & explantion!!!
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved