Check out a free preview of the full Java Fundamentals course

The "Polymorphism" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates a subclass's ability to define unique behavior while sharing behaviors of their superclass. A polymorphic object will execute its overriding method at runtime.

Preview
Close

Transcript from the "Polymorphism" Lesson

[00:00:00]
>> So we've talked about, a couple of the core principles of object-oriented programming. We've talked about encapsulation, we've talked about inheritance a third pillar is polymorphism, which is the ability to take multiple forms. Specifically, in object-oriented programming, polymorphism is where. A subclass can define their own unique behaviors and yet share some of the same behaviors of their superclass.

[00:00:37]
So an example of polymorphism is where an object can have a superclass type, which is a class, so animal is the superclass but it is an instance of a subclass Okay so here we have an object called Dog it's defined as type animal which is a superclass. But it is an instance of type dog which inherits from animal.

[00:01:08]
So we're able to do this because dog inherits from animal and therefore dog is an animal, right? Okay, so we're going to demonstrate an example of polymorphism between an animal, a dog, and a cat. All right, so this should be a fun exercise. So in our IDE, let's make a new class under the inheritance package.

[00:01:40]
And we'll call this one Animal. And in the Animal class let's just make one method that we'll call make sound and then we'll just make a print statement here that says unknown animal sound frightening right All right, and then let's make another class under inheritance that will call Dog.

[00:02:33]
Dog will, of course, inherit from animal. And, let's override, that make sound method. And in this one, we'll print woof And let's also make another method that is specific to the dog. We'll call this one fetch. And then fetch will just print out fetch is fun. All right, and then let's create one more class.

[00:03:42]
We'll call this one Cut. It will also inherit from animal We want to override that makes sense. The cat says meow. And we'll add a specific methyl for cat called scratch. And this will print out. I don't know I am a cat I scratch things. I scratch things.

[00:04:39]
Okay, this is our inheritance, let's create one more class just to test inheritance out. We'll make a zoo, new class called Zoo. Maybe it should be called farm? I don't care, whatever you want. All right, so we'll create a main methyl in Zoo. And I wanna create a new Dog.

[00:05:08]
So we'll create a Dog. His name is rocky. And if we say rocky. Then we have fetch and make sound, right? Those were the methods that were in the dog class. Okay, so let's say rocky is going to fetch. And let's just test this out. Make sure everything is set up properly.

[00:05:38]
Great, we have fetch. It's fun, perfect. Now let's say rocky.make sound, make sure that he's woofing, run this, and yes, he's woofing, perfect. So we're all on the same page, right? Regular inheritance there. Okay, now when we get to the polymorphism part, let's make a new Dog but they're of type animal her name is Sasha we're gonna instantiate Sasha as a dog All right, now when we say sasha.makeSound.

[00:06:27]
What method do you think is gonna be called? The one from animal or the one from dog? How many people say animal? It's really just a gamble at this point. How many people say animal? How many people say dog? Okay, people are just on the edge of their seats waiting to see what happens.

[00:06:51]
>> Someone said animal online.
>> Animal. Okay all right let's see. So we're gonna run this Sasha is an animal dog and Wolf was the one that was. That was executed. All right, okay, good, make sense? Now let's get into some really wonky polymorphism we are going to change sasha from a dog to a cat.

[00:07:25]
I know. And if we say sasha.makeSound, what sound will she make? Meow. Very good, very good. Okay. Excellent polymorphism really powerful tool in java you see how we can change from one class to another. The key points to remember is that an object can have a superclass type, but a subclass Instance.

[00:08:10]
If a method is overwritten by a subclass, then it's going to execute the method the overriding method.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now