Abstract Classes and Interface in C# .Net

Ratings:
(4)
Views:807
Banner-Img
  • Share this blog:

 

Abstract Classes and Interface

  An 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 a body is called an “Abstract method”. Abstract methods is also called Rules.

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

that is Abstract class contains methods with Body and methods with our body also, then these are called “partial Abstract class”.

All Abstract methods must be overridden in the derived classes. An abstract class provides a set of Rules (Abstract method), which must be followed (override) in derived classes.

An abstract class is not insatiable (an 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 ();  

Interested in mastering .NET? Learn more about ".NET Training" in this blog post.

Example of Abstract class

  Open windows form 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 ();

print ();
find Area ();
Display () ;
Shape s = new circle ();

print ();
find area ();
}  

Interface

  The interface is similar to Abstract classes.

The interface contains only abstract methods.

The 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 needs to be implemented in multiple locations with the same class name, then their classes need to be declared as partial classes.  

Example of Interface and Partial classes   

Open windows form 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 T

est // : i2 not must

{

Public void Print ();

{

Message Box. Show (“From print”);

}

}

//test *F5   Code for Button 1 _ click

{

Test t = new Test ();

read ();
print ();
i2X = new Test ();

read ();
print ();
  *F5

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox