c# Interface question - noob

J

jason

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
 
P

Paul E Collins

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.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

|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
 
C

Claes Bergefall

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
 
J

jason

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???
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
J

jason

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top