-
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?
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
|