Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Can we talk about polymorphism?

+5 votes
The study sheet said to discuss vocab on the Q&A...

Inheritance and polymorphism are linked, right? A class that's a subclass of another inherits the superclass behaviors/fields, then are such classes that share behaviors/code polymorphic?
asked Sep 7 in CSC 285 (F17) by Christian Elliott (254 points)

3 Answers

+4 votes
Best answer
Don't forget about interfaces though! Classes that implement the same interface share all the methods in the interface, so you've got polymorphism there too. For example, if you had a Flyable interface, you could have Mosquito and Robin both implement Flyable, create an array of Flyables that contains Mosquitos and Robins, and tell everything in that array to fly(). This comes in handy when different classes have some similar behaviors, but don't make sense to link by inheritance.
answered Sep 7 by Jonathan Meir (880 points)
selected Sep 7 by Christian Elliott
+2 votes
I think polymorphism has more to do with when you use a subclass if it was an object of it's superclass. For example, if jet is a subclass of airplane and you have a list of airplanes, then you can put a jet in the list without a problem.
answered Sep 7 by Jack Cannell (434 points)
+1 vote
Yeah, that's exactly it. Polymorphism means that one class shares behaviors with another class so it can stand in for that one.
answered Sep 7 by Maegan Patterson (424 points)