O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with typecasting and polymorphism

We can use the same method—that is, the same name and arguments—to cause different things to happen according to the class on which we invoke the method. In object-oriented programming, this feature is known as polymorphism.

For example, consider that we defined a talk method in the Animal class. The different subclasses of Animal must override this method to provide their own implementation of talk.

The Dog class overrode this method to print the representation of a dog barking—that is, a Woof message. On the other hand, a Cat class will override this method to print the representation of a cat meowing—that is, a Meow message.

Now, let's think about a CartoonDog class that represents a dog that can really ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required