0

We are learning about both of these things in Java class right now. I believe I understand the basic aspects of both, but not sure about how Casting ends up limiting Algorithm Reuse. Our teacher said we need to know this for the test next week. Can anyone explain this?

user3067497
  • 29
  • 1
  • 4

2 Answers2

1

If you cast you are limiting your algorithm to only work with one Class (or it's children). If you were instead to use an Interface you would be able to accept a greater variety of Objects that themselves implement that Interface. Much more flexible.

Here is a related SO question: Explaining Interfaces to Students

Community
  • 1
  • 1
indivisible
  • 4,892
  • 4
  • 31
  • 50
0

When you use casting in your code, you must know the exact type that you cast to (during the code write phase). Hence your code can't be reused in the future with different types. Always remember to program to interface instead of to specific type.

Maxim Kirilov
  • 2,639
  • 24
  • 49