Docsity

Understanding Polymorphism and Binding in Java: Upcasting, Downcasting, Cloning, Slides of Java Programming

Partial preview of the text

Download Understanding Polymorphism and Binding in Java: Upcasting, Downcasting, Cloning and more Slides Java Programming in PDF only on Docsity!

Polymorphism

Announcements

Homework 4 due now Homework 5 posted due in 2 weeks

Early/late binding

Early binding chooses methods at compile time (runs based on object type in code)

Early/late binding

Late binding chooses methods at run time (can pick most appropriate method) What determines early/late binding?

  • final and static modifiers use early binding (and private methods)
  • Everything else uses late binding (See: Binding.java and BindingParent.java)

Upcasting/downcasting

When can you upcast (without error)?

  • Anytime, no problem! When can you downcast (without error)?
  • You can only correctly downcast objects if they were upcast at some point
  • Use instanceof to check if downcast possible (See: UpDownCasting.java) Docsity.com

Downsizing?

clone

You can implement clone() using a copy constructor (better way in Ch. 13) You should make sure that when copying, you use clone() instead of a class's copy constructor (See: Animal.java and Dog.java)

Battleship