• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface reference variable as method paramater?

 
Hansjoachim Vonhortenschiem
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hansjoachim Vonhortenschiem
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic