Question

Regarding polymorphism, both my book and youtube videos skim over the fact that you can do this: Animal max = new Dog();.

Now I've seen one example where you create a list using an array with this scenario that makes perfect sense to me.

However, in my previous example, I cannot fathom why I wouldn't just write

Dog max = new Dog();

What is max gaining by setting his variable as Animal, assuming Dog:Animal inheritance?

Was it helpful?

Solution

In your example, there really isn't an advantage. A more typical scenario which would emphasize the power of polymorphism is the following:

public class Foo
{
    public Animal GetAnimal()
    {
        return new Dog();
    }
}

public class Bar
{
    public void DoSomething()
    {
        Foo foo = new Foo();
        Animal animal = foo.GetAnimal();
    }
}

So, why not just return a Dog in the method instead of an animal? Because good coding practices tell us to return the least derived class that would be sufficient enough in providing access to the members. An even more likely scenario would be to return an interface instead:

public class Foo
{
    public IAnimal GetAnimal()
    {
        return new Dog();
    }
}

In a real-world scenario, the most often case I've run across is a method which returns IEnumerable<T>, but really returns an array. Why not just return the array? Because the consumer only needs a sequence of elements, not the underlying data structure.

OTHER TIPS

Polymorphism touches a lot of other concepts related to object oriented programming.

One of them is coupling. If a class (i.e., Vet has a method that takes a Dog as a parameter (void Treat(Dog dog)), then these two classes are tightly coupled - Vet depends on Dog.

By accept an Animal as an argument instead (void Treat(Animal animal)) you achieve a higher level of decoupling. Vet now depends on Animal, which is a more abstract concept, as opposed to Dog which is a concrete concept.

In order to further understand the benefits of this, study the 5 SOLID principles and "high cohesion, loose coupling".

And this brings us to another concept - flexibility. The Treat method is now a lot more flexible, it can work with any kind of animal.

This reason is you can later change it to a different animal.

For e.g)

Animal pet = new Dog();

// you do some thing else .. assume your pet is currently dog and your apartment can collect this detail as an animal.. 
// now your dog ran away :D and you have cat as your pet 
// all you have do is just change the variable to point to cat instance

pet = new Cat(); //ofcourse Cat should inherit Animal

So its a flexibility

Now assuming, your apartment can do a lot of common methods to pets

e.g)

public Pass issuePass(Animal pet)
{
  return new Pass(pet.gettype(),pet.getowner(),pet.getname());
}
//you dont need to change this method.
//otherwise you need to create new methods unneccesarily

public Pass issuePass(Dog pet)
{
  return new Pass(pet.gettype(),pet.getowner(),pet.getname());
}
    public Pass issuePass(Cat pet)
{
  return new Pass(pet.gettype(),pet.getowner(),pet.getname());
}

You are right, when you hard code the instantiation of new Dog() it is not as useful. You gain a lot more value when you hide what kind of animal is being stored in the animal variable. This completely decouples the implementation from the interface.

Animal animal = AnimalFactory.GetAnimal();

Now the animal could be a dog or a cat. Since both are animals you don't need to know what kind of animal it is because all animals support a basic interface which you interact with.

This makes it easy to swap out components without the program missing a step as well as facilitates easy testing.


Even if you do not abstract away the concrete type, I would say it's good practice to always program to the most general interface possible. If you are not using any features that are specific to dog then why store it in a dog variable. Storing it in an animal makes it more clear that only animal features are used. Future maintainers can see that and know that it is safe to swap out dog with another animal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top