Ineritance: Values of parent/base class

D

Darren.Ratcliffe

Hi guys

Posted what was probably a rather confusing post about this the other
day, so I am going to have another go and see if I can make more sense.
This sis purely a

I've got a base class called animal, and from within animal you can
access lots more classes such as feline, canine, reptile and
amphibian.....

To access canine, you are going to go through animal. Animal has lots
of public properties such as height and weight that we can access from
our aspx.

So first of all I want to do animal.configure(iHeight, iWeight).....

Inside canine, I would like to be able to access the properties of the
animal object, so i can find out how big it is, how tall it is etc etc
- does that make sense?

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get animal to access the properties of
animal.

Can you help?? Awaiting you is my eternal gratitude ;)

Many thanks

Darren
 
D

Darren.Ratcliffe

*** SORRY! ***

In correction to the above, the next to last paragraph should read:

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get CANINE to access the properties of
animal.

Thanks

Daz
 
T

TheSteph

Can you post some code ???

Do you dervie Canine from Animal ?

class Canine : Animal
{

}


Steph.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I've got a base class called animal, and from within animal you can
access lots more classes such as feline, canine, reptile and
amphibian.....

You do not access them, you derive Animal in all those other classes
To access canine, you are going to go through animal.

What you mean with this?
Inside canine, I would like to be able to access the properties of the
animal object, so i can find out how big it is, how tall it is etc etc
- does that make sense?
Yes.
You will be able, unless you declare them as private

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get animal to access the properties of
animal.
Can you help?? Awaiting you is my eternal gratitude ;)

Post a piece of code whith what the problem is and the error you are getting
 
G

Guest

This is definitely the correct approach. Canine should be derived from
Animal. Its not a child of Animal, its a specialization of Animal.

So if...

public class Animal
{
public int weight;
public int height;
}

public class Canine : Animal
{
public int teeth;
}

Canine dog = new Canine();
Console.WriteLine(dog.weight);

// polymorphism in effect...
Animal animal = new Canine();
Canine dog2 = (Canine)animal;
 
D

Darren.Ratcliffe

Yes Steph

Basically, Canine belongs to Animal.

So, inside Canine, I want to be able to access properties of Animal.

For example:

class Animal
{
// Declare the child object (I presume this is correct)
Canine Canine = new Canine();

public string sName = "Rex";
}

Class Canine
{
Public String myFunc()
{
return Animal.Name + " is our returned value"
}

}

Thanks very much for your help.

Darren
 
D

Darren.Ratcliffe

Hi Ignacio

Thanks for trying to help!

I have posted a code snipped in reply to Stephs reply - please could
you take a look at it there?

Many many thanks.

Darren :)
 
M

Marc Gravell

What you are trying to do is presumably inheritance, not encapsulation; to
give a more complete example:

Marc

======

using System;

static class Program {
static void Main() {
Canine rex = new Canine();
rex.Name = "Rex";
Console.WriteLine("Rex speaking: " + rex.Speak()); // use the method
inherited from all animals
Console.WriteLine("Rex barking: " + rex.Bark()); // use the
canine-specific method
Animal animal = rex; // note that this does *NOT* create a new
object -
// we are still looking at the same rex, but just using the generic
animal API
Console.WriteLine("Animal speaking: " + animal.Speak());
Console.ReadLine();
}
}

abstract class Animal { // abstract: doesn't declare a concrete (creatable)
type - but only a base class
private string _name;
public string Name {get {return _name;} set {_name = value;}}
public abstract string Speak(); // force all Animal types to provide
this
}

class Canine : Animal {
public string Bark() { // this method is specific to Canine objects
return Name + " says \"Woof\"";
}
public override string Speak() {
return Bark();
}
}
 
D

Darren.Ratcliffe

Hi Marc, and to everyone else.

Well, after a good hour and a half of brushing up on OOP with Pro C#
2005 and the .NET 2.0 Platform (Apress) that I bought, I have got it.

What I want to do is set up my child class (canine) as Canine:Animal.

Then, I can access the protected properties of animal from within
canine by doing this.AnimalProperty.

Now I know that, even I think I sounded stupid before ;)

Many thanks for all your help - it is appreciated :)
 
J

Joanna Carter [TeamB]

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

Darren, it is very important that you learn the correct terminology; this
will really help you in asking for and understanding the help given in these
groups and elsewhere.

| Basically, Canine belongs to Animal.

This is incorrect. Canine doesn't "belong" to Animal, it is said to derive
or inherit from Animal.

Inheritance is described as an "is a" relationship between two classes. In
this case, a Canine *is* an Animal. This implies that a Canine is everything
that an Animal is plus some specialised behaviour; in fact, inheritance is
also known as specialisation.

When designing hierarchies of classes, it is sometimes useful to design from
a "bottom up" viewpoint. IOW, start by saying what ultimate classes you want
to deal with and then look for commonality in those classes which can be
"generalised" into a "superclass".

e.g.
Poodle, Siamese, Dachshund, Wolf, Dingo, Tiger, Lion

Start by inheriting Poodle and Dachshund from DomesticDog, then Wolf and
Dingo from WildDog, then inherit DomesticDog and WildDog from Canine. Now
you can continue by inheritingSiamese from DomesticCat, whilst Tiger and
Lion inherit from WildCat. Both DomesticCat and WildCat can then inherit
from Feline and then Canine and Feline can inherit from Mammal, which can
inherit from Animal.

Of course the above example may be overly fine-grained for the purpose of
the progam and part of good OO design is knowing how much behaviour goes in
what level of the hierarchy as well as how deep the hierarchy should be.

You can also use interfaces to reduce the depth of hierarchies, but that is
best left until you get the hang of class inheritance first :)

| So, inside Canine, I want to be able to access properties of Animal.
|
| For example:
|
| class Animal
| {
| // Declare the child object (I presume this is correct)
| Canine Canine = new Canine();
|
| public string sName = "Rex";
| }
|
| Class Canine
| {
| Public String myFunc()
| {
| return Animal.Name + " is our returned value"
| }
|
| }

This code also demonstrates that you still haven't quite got the hang of
terminology or how inheritance works.

| // Declare the child object (I presume this is correct)
| Canine Canine = new Canine();

This way of declaring a field of type Canine in the Animal class indicates,
not an "is a" relationship but a "has a" relationship, known as aggregation.
IOW, you are saying that any Animal *has* a Canine inside it; not quite what
you intended :)

| public string sName = "Rex";

This line will ensure that all instances of the Animal class will have the
Name "Rex".

| return Animal.Name + " is our returned value"

The Name field of the Animal class is only available from instances of that
class, not from the Animal type itself.

If you were not wanting to inherit, then you would have to, at least, place
a field of the Animal type in the Canine class, instantiate it and then use
that instance as a source of the Name property :

class Canine
{
private Animal animal = new Animal();

public Canine()
{
animal.Name = "Canine";
}

public string MyFunc()
{
return animal.Name + " is our returned value"
}
}

Note the difference in case of the Animal type and the animal field. This is
often the only way you will be able to differentiate between classes and
objects which are instances of those classes. You can, of course, adopt a
different naming convention but this is recognised as the "normal" Microsoft
recommended coding standard.

But as you really intended to use inheritance, then you would want to write
your classes in a different way. Note that it is not a good idea to have
public fields; you would normally use private fields and then surface the
value through a public property.

class Animal
{
private string name; // private field

public string Name // public property
{
get { return name; }
set { name = value; }
}

class Canine : Animal
{
public string MyFunc()
{
return Name + " is our returned value"
}
}

Because the Name property is declared as public in Animal, code in derived
classes can see and use it as if it were in the same class. However, because
the name field is declared as private, code in any derived class cannot see
it.

Private visibility means that only code in the declaring class can see it.

Protected visibility means that code in derived classes can see it as well
as the declaring class.

Public visibility means that any code in any class, even those not derived
from the declaring class can see it.

I order to use the above classes, you would have to do something like this :

public void Test()
{
Canine aDog = new Canine();

aDog.Name = "Rex";

string aDogsName = aDog.MyFunc();

// or you could just do :

string aDogsName = aDog.Name;
}

Does this help clear the fog ?

Joanna
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
class Animal
{
// Declare the child object (I presume this is correct)
Canine Canine = new Canine();

Not at all, it's completely wrong, what you are saying is that an Animal
CONTAIN a canine. You are NOT saying that a canine IS an animal

public string sName = "Rex";
}

Class Canine
{
Public String myFunc()
{
return Animal.Name + " is our returned value"

Wrong again. you are treating Name as a public static member of Animal
class. The correct construction is

return Name + " is our returned value";


look that Name is accessed as it was declared in Canine. It was really
declare in Animal but as Canine derive from Animal this makes the class
Canine the mix of Animal and Canine


IMO you have to hit hard a OOP book that explain the concepts of OOP
 

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