From the course: Computer Science Principles: Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use polymorphism using interfaces

Use polymorphism using interfaces

From the course: Computer Science Principles: Programming

Start my 1-month free trial

Use polymorphism using interfaces

- When you create a variable it has to have a type. Either you define the type or it's inferred by the value that is being assigned to it. When you have a variable that has a type you define the type based on a class. Remember, since everything is a class that means integer, Boolean, string, and everything else that we used before to define a variable is also a class. So if we create a variable that accepts integers we are typing that variable using the integer class. We can type variables to be any object we want, including objects based on classes that we create ourselves. So if I take a variable and set it to the cat type I can create an instance of a cat class and assign it to the variable. But what if I wanted to assign a fish to the variable? Because it is specifically typed to a cat I can't take an instance of a fish class and assign it. There is a way to deal with this. Instead of typing to a cat I can type the variable to the super class, animal. Since, by the definition of…

Contents