From the course: Object-Oriented Programming with Java

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Polymorphism in action

Polymorphism in action

- [Instructor] I'd like to show you a code example of polymorphism at work. I'm going to start with a country. In this country, I'm going to have a method for dancing. We're not going to do any real dancing, we're just going to print out. Okay, next I'm going to use this object in a class that I'm going to call United Nations. The United Nations is the class that I'm going to use to interact with my country, and you might be already guessing that there's going to be more than one of those. So to get that country dancing, I need to instantiate a country and then call the dance method. Whoohoo we're dancing. Nothing too crazy just yet, but I'm going to start making some other countries. So lets start with one of my favorite countries, Ireland. I'll put Ireland down here. Okay, now I'm going to have Ireland extend country. So that means I can instantiate Ireland and also call the dance method. It's been called twice…

Contents