• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Abstract Classes and Interface in C# .Net

Abstract Classes and Interface

 

Abstract is a keyword

When a class is not providing full functionality as per the requirement to declare that classes as “Abstract”.

A method without body is called as “Abstract method”.

Abstract method are also called as Rules.

When a classes contains at least one Abstract method , then the class must be declared as “Abstract method”.

that is Abstract class contain methods with Body and methods with out body also, then these are called as “partial Abstract class”.

All Abstract methods must be override in the derived classes.

Abstract class provides a set of Rules (Abstract method), which must be followed (override) in derived classes.

Abstract class are not insatiable (object cannot be created), but a reference can be created .

Test t = new test () // object

Test x ; //reference

 

‘t’ contains its own memory but ‘x’ does not contain its own memory.

Always reference work based on child  class memory.

Shape  s = new circle ();

 

Example on Abstract class

 

Open windows forms application project.

Place a button

Code in GD

 

Abstract class shape

{

Private int x = 10; y = 20;

Public  void print ()

{

Message Box . Show (X + “” y);

}//print

Public Abstract void find area ();

}//shape

Class circle : shape

{

Provide int r = 10;

Public override  void find area ()

{

Double x = 3.14 * r* r;

Message Box. Show (“Area is ” + X);

}// find Area

Public void Display ()

{

Message Box. Show (“thanks”);

}

}//circle

àcode for Button 1_ click

Private void button1 _ click (object sender, event prgs e)

{

Circle c = new click ();

  1. print ();
  2. find Area ();
  3. Display () ;

Shape s = new circle ();

  1. print ();
  2. find area ();

}

 

Interface

 

Interface are similar to Abstract classes.

Interface contain only abstract methods.

Interface supports a structure like multiple inheritance

Syntax to write an interface

 

Interface int name

{

Void read ();  à CLR-àpublic abstract àvoid Read ()

Void Print ();

}

All interface methods are by default public abstract methods.

 

Syntax to use Interface in classes 

 

Screenshot_58

 

Screenshot_59

 

 

Screenshot_60

 

Partial class Test

{

Programmer 1

}

 

Partial class Test

{

Programmer 2

}

 

Partial Classes

“Partial” is a keyword.

When a class need to be implemented in multiple locations with same class name , then there classes need to be declared as partial classes.

 

Example on  Interface and Partial classes 

 

Open windows forms Application project

Place a button

Code in GD

 

Interface i1

{

Void read ()

}

 

Interface i2 :i1

{

Void Print ();

}

Partial class test : i2

{

Public void read () // overriding method

{

Message Box. Show (“From read”);

}

}//Test

Partial class Test // : i2 not must

{

Public void Print ();

{

Message Box. Show (“From print”);

}

}//test

*F5

 

Code for Button 1 _ click

{

Test t = new Test ();

  1. read ();
  2. print ();

i2X = new Test ();

  1. read ();
  2. print ();

 

*F5

Summary
Review Date
Reviewed Item
Abstract Classes and Interface in C# .Net
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Abstract Classes and Interface in C# .Net"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.