Interface with example

Posted by Ppathi under C# on 2/28/2012 | Points: 10 | Views : 1295 | Status : [Member] | Replies : 5
Hi,

Any one please explain about interface with simple example

Thanks & Regards,
Pasupathi G
No Pain No Gain



Responses

Posted by: Pmithun121085 on: 2/28/2012 [Member] Starter | Points: 25

Up
0
Down
Interface is a class with no implementation just like abstract class.
This example uses interfaces:
-One interface introduces two properties
- One interface introduces a method
-One interface inherits from two interfaces but adds a new property
-One class inherits from an interface

Source File: Preparation.cs

public enum SportCategory
{
SinglePlayer,
Collective,
Unknown
}

public interface ICourtDimensions
{
double Length { get; set; }
double Width { get; set; }
}

public interface IBall
{
int NumberOfPlayers
{
get;
set;
}

string NameOfSport
{
get;
}

void SportCharacteristics();
}

public interface ISportType : IBall, ICourtDimensions
{
SportCategory Type
{
get;
}
}

Source File: Sport.cs

using System;

public class SportBall : ISportType
{
int players;
string sport;
SportCategory _type;
double Len;
double Wdt;

public SportBall(int nbr, SportCategory tp, string name)
{
players = nbr;
_type = tp;
sport = name;
}

public int NumberOfPlayers
{
get { return players;}
set { players = value;}
}

public string NameOfSport
{
get { return sport; }
}

public SportCategory Type
{
get { return _type; }
}

public double Length
{
get { return Len; }
set { Len = value; }
}

public double Width
{
get { return Wdt; }
set { Wdt = value; }
}

public void SportCharacteristics()
{
Console.WriteLine("Sport Characteristics");
Console.WriteLine("Name of Sport: {0}", NameOfSport);
Console.WriteLine("Type of Sport: {0}", Type);
Console.WriteLine("# of Players: {0}", NumberOfPlayers);
Console.WriteLine("Court Dimensions: {0}m x {1}m", Len, Wdt);
}
}

Source File: Exercise.cs

using System;

class Exercise
{
static void Main()
{
SportBall volley = new SportBall(6, SportCategory.Collective, "Volley Ball");
volley.Length = 18;
volley.Width = 9;
volley.SportCharacteristics();

Console.WriteLine();

SportBall tennis = new SportBall(1, SportCategory.SinglePlayer, "Table Tennis");
tennis.Length = 23.7;
tennis.Width = 8.25;
tennis.SportCharacteristics();

Console.WriteLine();
}
}
This would produce:

Sport Characteristics
Name of Sport: Volley Ball
Type of Sport: Collective
# of Players: 6
Court Dimensions: 18m x 9m

Sport Characteristics
Name of Sport: Table Tennis
Type of Sport: SinglePlayer
# of Players: 1
Court Dimensions: 23.7m x 8.25m


Mithun P

Ppathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepkumar417 on: 2/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Go through below link

http://www.csharp-station.com/Tutorials/Lesson13.aspx

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Ppathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 2/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Check this link
http://www.functionx.com/csharp1/examples/interface.htm .
http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Ppathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ppathi on: 2/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Thanks lot guys

Thanks & Regards,
Pasupathi G
No Pain No Gain

Ppathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 2/29/2012 [Member] Bronze | Points: 25

Up
0
Down
namespace SampleApp

{
interface IShape
{
double Area();
}

class Circle : IShape
{
double r;

public Circle(double radious) { r = radious; }

public double Area()
{
return 3.14D * r * r;
}
}

class Rectangle : IShape
{
double l, w;

public Rectangle(double length) { l = length; w = length; }
public Rectangle(double length, double width) { l = length; w = width; }

public double Area()
{
return l * w;
}
}

class Triangle : IShape
{
double s1, s2, s3;

public Triangle(double side1, double side2, double side3) { s1 = side1; s2 = side2; s3 = side3; }

public double Area()
{
double a = (s1 + s2 + s3) / 2;
return Math.Pow((a * (a - s1) * (a - s2) * (a - s3)), 0.5D);
}
}

class RightAngle : IShape
{
double h, b;

public RightAngle(double b, double height) { this.b = b; h = height; }

public double Area()
{
return 0.5D * h * b;
}
}

class Cylinder : IShape
{
double r, h;

public Cylinder(double redious, double height) { r = redious; h = height; }

public double Area()
{
return 2 * 3.14D * r * r * h;
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
IShape s = new Circle(25);
System.Diagnostics.Debug.Print("Area of Circle: {0}", s.Area());

s = new Rectangle(12);
System.Diagnostics.Debug.Print("Area of Square: {0}", s.Area());

s = new Rectangle(12, 3.5D);
System.Diagnostics.Debug.Print("Area of Rectangle: {0}", s.Area());

s = new Triangle(10, 11, 12);
System.Diagnostics.Debug.Print("Area of Triangle: {0}", s.Area());

s = new RightAngle(3, 4);
System.Diagnostics.Debug.Print("Area of Rightangle: {0}", s.Area());

s = new Cylinder(3, 4);
System.Diagnostics.Debug.Print("Area of Cylinder: {0}", s.Area());
}

}
}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ppathi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response