473,394 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1582
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: administrata | last post by:
Is it possible? I tried... I = "John" print \ """ I used to love pizza"""
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
1
by: petar petrov via DotNetMonster.com | last post by:
Hi to all, I need to implement the above interface in C#.NET managed application. Since I am a noob with COM i looked at MSDN article of how to invoke COM from inside managed code. However the...
12
by: you | last post by:
I may be just stupid, but I don't understand the point of using an Interface. I am going through a couple of books at the moment trying to teach myself a little vb.net. Just for the heck of...
2
by: Link360 | last post by:
Im a complete noob and im proud of it. I am excited in learning everything about the C++ language. Right now im trying to make tic-tac-toe. Go ahead laugh. here is what i have so far ...
5
by: rh1200la | last post by:
Noob question here. I'm creating an interface with various properties, but I also want it to include a <listof a particular object. What is the correct syntax to do this in an interface? ...
5
by: Hydrogenfussion | last post by:
Hello and thank you for reading this. I have just started to learn C++ from www.cprogramming.com,its a great site! But i do not understand some of the terms. I am using Dev-C++. Can you tell me...
1
by: Fluffy654 | last post by:
First off I am a complete noob when it comes to doing anything with servers. I'm just beginning to learn today because I need to start adding SSI to my websites. I apologise in advance if I am making...
3
by: Armin Zingler | last post by:
Hi, sorry for this C++ noob question... I'm coming from VB.Net actually and I am trying to do the same in C++ (/CLR) as I do here (pseudo code partial): class C implements I1 implements I2...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.