Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is happening with inheritance in my example? And, what is the proper terminology in c#?

Tags:

c#

oop

I am learning OOP and have a question about what is exactly happening with the code below.

I have the classic Dog Animal example going. Dog inherits Animal.

public class Animal
{
    public string Name { get; set; }

    public virtual string Speak()
    {
        return "Animal Speak";
    }

    public string Hungry()
    {
        return this.Speak();
    }
}


public class Dog : Animal
{
    public override string Speak()
    {
        return "Dog Speak";
    }

    public string Fetch()
    {
        return "Fetch";
    }
}

Both questions are based on this assignment: Animal a = new Dog();

  1. What is actually happening when I declare an Animal and set it to a Dog reference. Is there a specific term for this?
  2. When I call a.Hungry(), the output is "Dog Speak." If the output is "Dog Speak", why can I not call a.Fetch()? What is the term for what's happening here?

Any help and further reading on the particular topic would be greatly appreciated.

like image 417
Chace Fields Avatar asked Jan 18 '13 19:01

Chace Fields


People also ask

What is inheritance in C with example?

Inheritance is one of the key features of Object-oriented programming in C++. It allows us to create a new class (derived class) from an existing class (base class). The derived class inherits the features from the base class and can have additional features of its own.

What is the inheritance in C language?

Inheritance is the ability to define new classes based on existing classes in order to reuse and organize code. You can easily implement single inheritance in C by literally embedding the inherited class attribute structure as the first member of the derived class attribute structure.

What is inheritance give proper example?

Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs.

Which is the correct syntax of inheritance in C?

Which is the correct syntax of inheritance? Explanation: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name.


1 Answers

  1. This is an "upcast". In C# there is an implicit conversion from any type to any of it's base types, so you don't need to do anything to treat a Dog as if it were an Animal. (Thanks to Matt Burland for reminding me that this is the appropriate term.)
  2. Because the type of the variable is Animal, and as such you can only access members that the compiler knows an Animal can access, i.e. Speak and Hungry. It doesn't know that the Animal is a Dog, so it can't call Fetch. The variable would need to be of type Dog for you to be able to call Fetch on it.
like image 103
Servy Avatar answered Oct 26 '22 23:10

Servy