简体   繁体   中英

Which Java language mechanisms allow object reference variable types to be different than the type of object it refers to?

The questions asks

"What two Java language mechanisms allow the type of an object reference variable to be "different" than the type of the object to which it refers? Give specific examples to illustrate. In what sense are they not different at all?"

I am not sure if I am understanding this correctly. At first I thought it would be Polymorphism but doesn't that only determine which object a method will reference when there are multiple objects?

Then I think it is upcasting and downcasting. Since that seems like the correct answer.

I am not sure if I am missing another mechanism. Can someone help me clarify this, my book really does not do a good job of explaining this for me

You are on the right track with polymorphism although I don't think you understand it correctly. It means that one object can have more than one form. To have more than one form the object must be referred via a different type. There are two ways (at least) that you can make an object such that it can be referred to in multiple ways. As Anthony says, think Extending and Implementing.

I would have to say Interface and Inheritance. Consider...

List<String> list = new ArrayList<String>();

...the concrete ArrayList is indeed different than what list is declared to be.

See definition for polymorphism.

Ability to exist in many forms

And the concept it late binding. See this. http://en.wikipedia.org/wiki/Late_binding#Late_binding_in_Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM