PDA

View Full Version : [SOLVED] Confused by class inheritance in C#



liquidbee
October 31st, 2009, 03:33 PM
The idea is pretty simple:

create a fruit and set it's color/size
add chemicals which would change the fruit color


What exactly do I need to add to be able to access Apple objects from Chemicals class ?


namespace Fruit
{

class Apple
{
string color;
int size;

public Apple(string icolor, int isize)
{
color = icolor;
size = isize;
}
}

public class Chemicals
{
public Chemicals()
{

}
}

}

PaulM1985
October 31st, 2009, 03:55 PM
The idea of inheritance is that a class that inherits from another class is a specific type of thing it is inheriting from. So if Class_B inherits from Class_A, Class_B is a more specific type of Class_A. So in your example, it would be more likely that you would create a class Apple, which would inherit from the class Fruit, because Apple would be a specialised type of fruit.

What you need in your example is that Chemical should have a reference to a Fruit object.

So somewhere in your Chemical class you would have:

Fruit aFruit = new Fruit("Red", 1);

If you wanted to change the colour of this fruit, in the Fruit class you would need a method:
void setColour(String newColour)

which would change the colour of the fruit. In your Chemical class, to change the colour of the fruit, aFruit, which has already been defined, you would call the setColour method of Fruit, doing:

aFruit.setColour("Blue");

Hope this clears up the inheritance issue for you and you understand what is needed to solve the problem.

Paul

liquidbee
October 31st, 2009, 05:59 PM
I see. Thanks for an explanation - finally I got it running the way I wanted.

Frak
October 31st, 2009, 06:07 PM
You could also use colon inheritance. By using a colon next to your class definition, it inherits from the specified class/interface.


public class Chemicals : Apple
{
...
}

If I'm misconstruing the question, tell me now. Paul's answer, though, sounds more "correct".

EDIT
Just a quick note, using colon inheritance won't inherit private data, so be aware.

liquidbee
October 31st, 2009, 06:27 PM
You could also use colon inheritance. By using a colon next to your class definition, it inherits from the specified class/interface.


public class Chemicals : Apple
{
...
}

If I'm misconstruing the question, tell me now. Paul's answer, though, sounds more "correct".

EDIT
Just a quick note, using colon inheritance won't inherit private data, so be aware.

Honestly, the whole problem was just my imagination. I thought namespace works pretty much in the same way as classes do ( as an example, UbuntuForums class would have 2 subclasses - users and moderators ).