-
August 1st, 2014, 07:34 AM
#1
An Issue with Inheritance
I am doing some testing with inheritance, and I ran across this little bit of what I consider weirdness. Here is my class layout:
Code:
class animal
{
}
class dog : animal
{
public dog()
{
Console.WriteLine("woof!");
this.bark();
Console.ReadLine();
}
public void bark()
{
Console.WriteLine("woof! woof!");
}
}
I instantiate it like so:
Code:
animal dog = new dog();
Ok, so I noticed something a while back when instantiating an object with the type of the base class and making it a new instance of a derived class: the derived class's overrides and unique methods are not available to the new object. However, in this case, I am seeing that they are available through the derived class constructor. If I do this afterward:
It doesn't work.
- Why is this?
- Is it useful in a way that I'm not seeing?
- Is there a way to access those methods again using that new object?
-
August 5th, 2014, 05:53 AM
#2
Re: An Issue with Inheritance
i didn't double check this code so just consider it pesudo code
im sick atm but since no one replied ill just toss out a basic answer
you haven't actually defined those methods in the animal class
so you cant call something you haven't defined inheritance works of the parent
to say animal is the predacessor or parent of dogs cats cows ect.
you would do it like so
class Animal
{
public string Noise{get;set;}
public virtual void Speak()
{
Console.WriteLine(Noise);
}
}
class Dog : Animal
{
public Dog()
{
base.Noise = "woof !"
}
public override void Speak()
{
base.Speak();
}
}
so the idea is if you have a cat class when you call Speak it will instead meow
if you have a dog class it will bark when you call speak but now you just call Speak for either
if you urself or another programer uses your class's and sees that there is a dog cat or animal class
in use and they all inheirit from animal he knows
all these class's should have at least those public methods available to him or
overriden safe versions particular to the class
in other words he or you can just call mycat.Speak() or mydog.Speak()
also if you want to make a array or list of animals and add to that list cats or dogs ect
they may be in reality defined by different types
e.g.
public List<Animal> animals = new List<Animal>();
animals.Add(new Cat());
animals.Add(new Dog());
animals[0].Speak(); // meow
animals[1].Speak(); // woof
cool right
now interfaces are c# answer to multiple inheritance
maybe little better a idea of it then older languages takes on it
Last edited by willmotil; August 5th, 2014 at 02:39 PM.
-
August 5th, 2014, 08:00 AM
#3
Re: An Issue with Inheritance
I hope you feel better.
This makes sense - essentially in the case that I want to use the animal class, the others cannot have any unique methods(that I can expect to use after casting), but their properties can differ, causing the base methods to behave differently.
My questions then are these: why do you call the base version of the speak method/assign to the base property? If the dog and cat classes are inheriting versions of these properties, can't the method/property from the derived class be used? Is there a specific benefit to doing it that way?
I have actually seen this selectively through code examples here and there, specifically when a derived class will call the base constructor. In this case, however, you are overriding the Speak method and the override simply calls the base version. Acknowledging that I am new to this and really don't know all the 'why's, this doesn't make sense to me. What am I missing?
Last edited by decimer; August 5th, 2014 at 08:02 AM.
-
August 5th, 2014, 09:35 AM
#4
Re: An Issue with Inheritance
thank's i hope i feel better too. arrggg
My questions then are these:
why do you call the base version of the speak method/assign to the base property?
to show that you can if you desire simply use the original implementation that you defined for animal
you might make a weird class and not yet wish to specify the noise it makes
or do that at a later time. say you want the basic structure in place in the class though
to make a silly analogy ....
if animals noise is "grrr" a cat or dog may have evolved a new sound woof or meow
and maybe certain dogs can make weirder sounds like "wooooooooooooooooooof"
but a MissingLinkDog may look like a dog but still make a "grrr" noise.
so in that manner you have flexibility to tackle any problem were, this sort of solution is needed
If the dog and cat classes are inheriting versions of these properties, can't the method/property from the derived class be used?
yep it can
you can implement a completely new dog Speak method by specifying the override command on your own Dog Speak
put in the console writeline directly there with its noise string output msg and never call base.
or you can
Not define one at all and implement Bark() which is very specific to the dog class
and have both its inhearited Speak method and its individual Bark method
later down the line if some class may randomly call speak on anything that "Has A" animal base
its never going to fail with some weird error you know that all animals or children of animals can speak
or if you like that all things derived from a animal ie based on animal can speak
Is there a specific benefit to doing it that way?
i will answer this within the last question
which is better asked as in "what situation might i need or want to do it that way".
I have actually seen this selectively through code examples here and there, specifically when a derived class will call the base constructor
because the base constructor may do basic initialization or accept parameters that are meant to be overriden
its just a tool you can have functionallity in the base that you can call in thru any derived class you make
easilly and in a standardized manner.
In this case, however, you are overriding the Speak method and the override simply calls the base version. Acknowledging that I am new to this and really don't know all the 'why's, this doesn't make sense to me
the answer here is that im showing a ideological way to programatically answer
a set of both simple yet together complex inheritance relationship questions.
here is a good time to get super philosophical with some,,, er
dumb evolutionary questions that we might think at first are simple.
but in actuality require a change of thought to tackle.
lets ask darwin then,, just run right past him going down our programming road.
"can a dog bark" - yes
"can a cat bark" - no
"can a animal bark"- * well * ,,,, <<< now heres the real question ?
not all animals so No But,,,
a dog is a animal and it can bark so Yes,,, hummm (???)
lets ask a different question.
"can all animals make a noise" well ,,,
they all should be able to try to make a noise
if we say they can all Speak and some just put out silence then,,,
all animals cant bark but all animals can Speak and some "wooof"
so that way we exemplify
how you might use inheritance in the above manner or,,,
to show basically the power inheritance gives you as a tool
maybe not just for programing but also for the way you ask questions and think about solutions
more specifically
i showed it that way, as it is then dependent on how You want to use it
to show in a more complex case the ability to pass backwards functionality or
how to pass in a reverse direction what was functionally inherited
from the parent to the child then back to the parent
in context of the example
in the List of animals that was to follow that overriden constructor changes the property of the animal
the list of animals can then directly call its base Speak method
though they animals cannot directly call a Bark() method because it doesn't exist in the animal class
if we use a List array dictionary ect to store the functionally
we can access it from the more primitive animal class in the same way as our idea described above
in order to tackle the problem, were our question revealed the true answer
to say
we see that Speaking the "woof" is the exact same thing as Barking it
so a Animal that will make a Dog one day might be able to "woof" when it Speaks
but that same Animal cant Bark cause animals don't Bark
so here is also a real philosophy just not yet implemented in modern science
however it is or can be implemented in real programing.
one day you might need to do it or something similar "encapsulate duality to eliminate it"
Last edited by willmotil; August 5th, 2014 at 11:03 AM.
-
August 5th, 2014, 09:48 AM
#5
Re: An Issue with Inheritance
I understand. Awesome answer - thanks!
-
August 5th, 2014, 11:30 AM
#6
Re: An Issue with Inheritance
cool i mention interfaces cause after you fully understand inheritance is the best time to learn about them
because it is a continuation of the philosophical questions that come after them
it is the solution to question of such things as
"What then is a Animal ?"
when we say that ,,,
a Animal cant Bark but,,, we know a dog is a Animal and,,, it can Bark
so how can a Animal Bark but not be able to Bark ?
what kind of thing is a animal really here ?
we come to the conclusion that only the Dog or the Cat is real (or has unique data)
Cats Meow
Dogs Bark
Animals Never do Either instead they make sounds a basic shared action
or Speak ect but that's just a generalization of a actions
so like what animals do they themself's are in reality a Abstract idea "not real or concrete"
however the action itself is more functional reusable ideologically in the base interface or Abstract sense
to say we are just generalizing things or grouping them
i can ask my Dog to Speak it might "woof" back at me
i can ask my Cat to Speak it might "meow" back
but if i ask my Dog to "meow"
its gonna be like dude, ? , i may be a animal but im no cat buddy
so when we go back and say what is a animal,
what we are saying its a really a design blueprint of the shared attributes of actions among different things
given a generic or general name to allow communication
so that's our idea for a interface to turn that philosophy into a programing keyword
the keyword must be understood to be used and in turn we teach each other this philosophy
we say these things ,,, actions can apply to a lot of Different real things
but when we want to talk about these actions
they in themself's are really just abstract idea's or generilized ways to interface
with the basic shared action all of them should be able to perform named in the most base , basic way
so animal here is really a candidate for a interface
or a abstract class , and maybe should be instead a interface
once you start thinking about Speak vs Bark and Meow
then you see that Speak is a way to define a word that acts as a interface
to any real class object of Dog Cat ect... which has stated that it uses that idea set
so programers like to generalize even there idea's
so we just call them idea's but in reality if scientists or philosophers got there hands on them
they would be called theorys so...
humans "talk", Dogs "bark", Cats "meow"
but we know none of them truly do at all,
when you cut it down to the most basic idea they are just making sounds.
our Interface might describe this as a call ... Speak() to generalize it.
from something complicated into something so simple its almost dumb. but serves a brilliant purpose
to say it like this
animal Interfaces implement (has a) Speak Method so anything that uses a (is a) Animal interface
can Speak
our Interface Keyword is the description of both our design and communication interface usage ideology
Last edited by willmotil; August 5th, 2014 at 04:57 PM.
-
August 5th, 2014, 12:49 PM
#7
Re: An Issue with Inheritance
So then what you are talking about is like a more general sort of method, in which I can give a command to an object, and no matter what that command means to them, I get something back. It will be their version, but it will be something. As far as I can tell, though, the only real benefit to interfaces is a sort of QC of your classes and the ability to inherit more than one.
-
August 5th, 2014, 01:21 PM
#8
Re: An Issue with Inheritance
well these ideas are pretty powerful and highly used
were these examples are simplifications of the basic ideas
but you are in fact using interfaces in your basic programing
cause c# uses its own idea to full effect
for example
foreach(var obj in animals)
{
obj.Speak();
}
so above each animal will Speak
you can do this because List implements the IEnumerable interface
which lets you use foreach on the list of animals you made from cats and dogs
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
many c# class's implement there own interfaces,
in other words they are using over and over the stuff they wrote
so you are already using interfaces
you just haven't yet learned how or really needed to write your own
now this is bit off track
ignore it if you like but when you say why really in programing your more often going to ask
"how do i do this" then find the "why cant i" and "why did they make so and so"....
when you need it or when your stuck.
its really good to learn these keywords as a reference for those times
there are a lot of words used in programing
you wont really find tossed around else-were as if they were no big deal
(that's cause they are exactly defined there is no "depending on who you ask")
they imply simple idea's to handle problems with complex philosophy's behind them that have been well defined
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
the reason is because like our dog cat example questions to darwin
the question's are stupidly simple but unanswerable if not asked in the proper context
we were originally asking a Covariance and Contravariance question but darwin didn't get it.
that form of question was Invariant
so these words that are tossed around identify problems and solutions of a specific type
you might run into when programing your cool new idea
but you cant figure out how to make it work in code
to say if you can identify what a problem is
then you can identify the solution or a pattern to make it work
Last edited by willmotil; August 5th, 2014 at 05:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|