473,394 Members | 1,739 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.

Ineritance: Values of parent/base class

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

May 17 '06 #1
11 2840
*** 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

May 17 '06 #2
Can you post some code ???

Do you dervie Canine from Animal ?

class Canine : Animal
{

}
Steph.
<Da**************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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

May 17 '06 #3
Hi,

<Da**************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi guys
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
May 17 '06 #4
As I said, post the code
<Da**************@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
*** 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

May 17 '06 #5
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;
"TheSteph" wrote:
Can you post some code ???

Do you dervie Canine from Animal ?

class Canine : Animal
{

}
Steph.
<Da**************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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


May 17 '06 #6
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

May 18 '06 #7
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 :)

May 18 '06 #8
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();
}
}

May 18 '06 #9
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 :)

May 18 '06 #10
<Da**************@gmail.com> a écrit dans le message de news:
11**********************@j73g2000cwa.googlegroups. com...

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

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 18 '06 #11
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
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: RR | last post by:
I have two classes: class Base { function Insert() { // does stuff } }
2
by: Mattias B | last post by:
Hello! I have s quick question regarding multiple ineritance that I hope that someone will have the time to look at. I have the following classes that I try to compile and use: class A {...
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
12
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
14
by: Jo | last post by:
Hi, Is there a generic way to access the (virtual) functions in the base class of a class? Like in: if (!this->Foo(something)) return(((Base*)this)->Foo(something)); // for a normal member...
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.