469,909 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

c# Interface question - noob

I found the below example online, while trying to under Interfaces.

In layman's terms, can somebody explain what the purpose of this line
is:

void SportCharacteristics(); //LOCATED IN THE Iball interface.

... I read its a method without an implemetnation.. like an abstract I
think.. so how does it serve this project?
====

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(); // ****** PLEASE EXPLAIN THIS LINE ******
}

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

Aug 22 '06 #1
6 1452
ja***@cyberpine.com wrote:
In layman's terms, can somebody explain what the
purpose of this line is:
void SportCharacteristics(); //LOCATED IN THE Iball interface.
It indicates that any type implementing this interface must include a
method called "SportCharacteristics" that returns void. In the sample
you posted, this method is used to display information about the
sport. In other words, "all sports must have a standard method, with
this name, for displaying information about themselves".

Eq.
Aug 22 '06 #2
<ja***@cyberpine.coma écrit dans le message de news:
11**********************@i3g2000cwc.googlegroups.c om...

|I found the below example online, while trying to under Interfaces.
|
| In layman's terms, can somebody explain what the purpose of this line
| is:
|
| void SportCharacteristics(); //LOCATED IN THE Iball interface.
|
| .. I read its a method without an implemetnation.. like an abstract I
| think.. so how does it serve this project?

Interfaces can be regarded as pure abstreact classes, with absolutely no
code whatsoever., Therefore, all you see is the declarations of things like
properties and methods.

One of the differences between an interface and an abstract class is that
classes can implement more than one interface, whereas you can only inherit
from one abstract class.

Interfaces can be regarded as definitions of contracts which must be
fulfilled by any implementing class.

Another advantage is that interfaces can be implemented by differing
branches of a hierarchy, whereas abstract classes can only be inherited from
as the root of a hierarchy.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 22 '06 #3
An interface only defines the name, return type and parameters for a method
or property. A class can then chose to implement this interface and will
then provide the code so that it actually does something. In your case the
method (as well as the rest of the interface) is implemented by the
SportBall class. You can read more about interfaces in the help files.

/claes

<ja***@cyberpine.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>I found the below example online, while trying to under Interfaces.

In layman's terms, can somebody explain what the purpose of this line
is:

void SportCharacteristics(); //LOCATED IN THE Iball interface.

.. I read its a method without an implemetnation.. like an abstract I
think.. so how does it serve this project?
====

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(); // ****** PLEASE EXPLAIN THIS LINE ******
}

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

Aug 22 '06 #4
Could this just be a really bad and confusing example??

When I remove the line nothing changes in the results..

In my readings, the purpose to create the interface would be to methods
otherwise not available through them???
ja***@cyberpine.com wrote:
I found the below example online, while trying to under Interfaces.

In layman's terms, can somebody explain what the purpose of this line
is:

void SportCharacteristics(); //LOCATED IN THE Iball interface.

.. I read its a method without an implemetnation.. like an abstract I
think.. so how does it serve this project?
====

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(); // ****** PLEASE EXPLAIN THIS LINE ******
}

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
Aug 22 '06 #5
<ja***@cyberpine.coma écrit dans le message de news:
11*********************@i3g2000cwc.googlegroups.co m...

| Could this just be a really bad and confusing example??

It is quite a weak example in that, it declares an interface and then never
uses it.

| When I remove the line nothing changes in the results..

It won't. Removing a member of an interface will not stop the app from
working, wherfeas removing the implementing method from the SportBall class,
you will get a compilation error.

Try changing the test code to see what assigning an instance of a class to
an interface does.

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

Console.WriteLine();

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

Console.WriteLine();
}
}

Then try to write your own class that implements ISportType :

public class MySport : ISportType
{
...
}

Just enter the empty declaration and see what the compiler will tell you.

If you right-click on the ISportType part of that class declaration, you
will be given an option to let the IDE fill in the members of the interface
necessary to fulfil the interface contract.

Then you should be able to add the following code to the test method :

{
...

ISportType mySport = new MySport(... your params);

mySport.SportCharacteristics();

Console.WriteLine();
}

| In my readings, the purpose to create the interface would be to methods
| otherwise not available through them???

Read my other post, an interface is a contract which classes fulfil, it is
*not* something that you add to existing classes, unless you really have to.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 22 '06 #6
Joanna Carter [TeamB] wrote:
>>Try changing the test code to see what assigning an instance of a class to
an interface does.
That's what I was looking for in the code.. an instance of the
interface and a reason for that line.

Please excuse my functional (and not OO) thinking - and my possibly
incorrect terminology, but the way I'm understanding the line is that
it's implementing a method in another class (hence the name
interface). Without that line, that method would not be available to
it.. However, is it correct to say, that because this program is not
making a direct refrence to that interface that it's really not doing
much. Afterall, sportsball has SportCharacteristics available to it.

I suspect If I do the above the example and that line makes more sense
and the line is then required.

Aug 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by administrata | last post: by
8 posts views Thread by Ivan Shevanski | last post: by
1 post views Thread by petar petrov via DotNetMonster.com | last post: by
2 posts views Thread by Link360 | last post: by
5 posts views Thread by rh1200la | last post: by
3 posts views Thread by Armin Zingler | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.