aspose file tools*
The moose likes Java in General and the fly likes Interface reference variable as method paramater? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface reference variable as method paramater?" Watch "Interface reference variable as method paramater?" New topic
Author

Interface reference variable as method paramater?

Hansjoachim Vonhortenschiem
Greenhorn

Joined: Mar 03, 2011
Posts: 15
It is my most earnest hope that one day I might come to this forum and "confidently" provide answers that may prove themselves to be beneificial to others, as opposed to coming here and always asking questions.

Till then..............
Here's a question:

Some random class that implements some random interface

Some other random class that implements some other random interface

the class Carrot delcares an instance variable of type interface-Fruit. ok
the Carrot constructor requires an arguement of type interface-Fruit. ok
the instance variable is set using the value of the arguement to the constructor. ok

My questions are thus:
1. is it fruitBasket the instance variable or fruitBasket the parameter that is accessing doStuff() ?
2. if it is the instance variable, then how in the world does it even know of the existance of doStuff() ?
// obvious... of course a Fruit reference variable could access doStuff() ......duhhh!
3. can a parameter even be used in such a context as that, and if so, could you explain the step by step ?
// not sure if a parameter can be used in such a manner, but if it can, then again, a reference of type Fruit can access doStuff()
4. in regards to the this used in the statement, is it interpreted as, "the object that is being constructed", or something else ?
5.if it is the object being constructed, then it is of type Carrot, but doStuff() is anticipating a type Vegetable. So how did that work ?

Again, I offer heartfelt gratitude for any replies. For without them I am surely up the compiler without a syntax....?

p.s, to compound the problem, the driver for this particular piece of code is as follows:

well, that's just great !

the Carrot constructor accepts interface-Fruit and we sent it class-Apple!
I'm begining to panic
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Fuman Choo wrote:
p.s, to compound the problem, the driver for this particular piece of code is as follows:

well, that's just great !

the Carrot constructor accepts interface-Fruit and we sent it class-Apple!
I'm begining to panic


Isn't an Apple a Fruit? The constructor wanted a Fruit and you gave it a Fruit -- seems fine to me...

This is where some of the power of object orient programming comes in. If this was your last topic, it would have been something like, the constructor wanted a duck, and you gave it a cartoon duck. Perfectly fine, because a cartoon duck is a duck.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Hansjoachim Vonhortenschiem
Greenhorn

Joined: Mar 03, 2011
Posts: 15
aahhh!
Thanks Henry....

I've been working on this all afternoon and it would seem that I only succeeded in mentally sabotaging myself

Of course the Carrot constructor wanted a Fruit, and we sent it an Apple. How simple is that? Now, if you will permit me, I would like to attempt, using your logic, the statement on line 11. of the Carrot constructor:
1. fruitBasket is a reference variable that refers to the Apple object that was passed to the Carrot constructor.
2. this same Apple object is now "part of" this new Carrot object, and as such, the Apple object refered to by fruitBasket can, as you would expect, be able to acccess the doStuff() method defined/implemented in class Apple.
3. the this that is passed to doStuff() does indeed refer to the current Carrot object under construction. And this implies that doStuff(), who is expecting a Vegetable as an arguement will in fact recieve a Carrot object. And I think we can agree that a Carrot is indeed a Vegetable.

Do I have it right or have I yet to find the truth of the matter ?

Perhaps I missed the forest because of all the stupid trees ?

Thanks for your replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface reference variable as method paramater?