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

An overview of polymorphism in java, focusing on early and late binding, upcasting, downcasting, and cloning. Early binding selects methods at compile time based on object type in code, while late binding chooses methods at runtime for more flexibility. Upcasting and downcasting are discussed, including when they are possible and the use of instanceof to check for downcasting. The document also covers the importance of implementing the clone() method correctly when copying objects.

Typology: Slides

2012/2013

Uploaded on 04/23/2013

sarmistha
sarmistha 🇮🇳

3.8

(22)

112 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Polymorphism
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

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