wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing an interface reference to a method expecting an Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing an interface reference to a method expecting an "interface implementor"" Watch "Passing an interface reference to a method expecting an "interface implementor"" New topic
Author

Passing an interface reference to a method expecting an "interface implementor"

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



Why is the above not allowed? Ball implements Bouncable (Ball IS-A Bouncable) so i thought i should have been able to pass it a Bouncable reference.

Thanks
veera sangham
Greenhorn

Joined: Dec 30, 2011
Posts: 11
we can not send interface reference to a implemented class type variable.
But we can send implemented class type variable to interface argument type variable

class Ball implements Bouncable{}

class ZiggyTest{

public static void main(String[] args) {
Bouncable bn = new Ball();
bounce(bn);
Ball b =new Ball();
bounce(b);
}
public static void bounce(Bouncable b){}

}
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

You need an explicit downcast here. It is similar to the assignment Ball b=(Ball)bn, which does not work without the cast. You got to modify the method call as bounce((Ball)bn);
Even if Bouncable is a class and you have 'class Ball extends Bouncable{} ', the same casting would be needed.
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
O. Ziggy wrote:

Why is the above not allowed? Ball implements Bouncable (Ball IS-A Bouncable) so i thought i should have been able to pass it a Bouncable reference.

Thanks


hello O.Ziggy,

about your program i run on eclipse or even i run through notepad it is not giving me any error it works fine

well check my code if you found any error then tell me ::

interface SampleInterface{

}

class ClassImpl implements SampleInterface {

}

public class InterfaceDemo {

public static void main(String[] args) {

SampleInterface si = new ClassImpl();
fun(si);//<-- your error was here right?

}

public static void fun(SampleInterface si1){

}

}

Thanking you.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Amey Ambulgekar wrote:
O. Ziggy wrote:

Why is the above not allowed? Ball implements Bouncable (Ball IS-A Bouncable) so i thought i should have been able to pass it a Bouncable reference.

Thanks


hello O.Ziggy,

about your program i run on eclipse or even i run through notepad it is not giving me any error it works fine

well check my code if you found any error then tell me ::

interface SampleInterface{

}

class ClassImpl implements SampleInterface {

}

public class InterfaceDemo {

public static void main(String[] args) {

SampleInterface si = new ClassImpl();
fun(si);//<-- your error was here right?

}

public static void fun(SampleInterface si1){

}

}

Thanking you.


no it is not the same.. i am passing a Bounceable to a Ball. You are passing a SampleInterface to a SampleInterface so in your case there is no conflict..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Come on, you shouldn't get such simple things backwards any more. Here's an equivalent:


And you know why the compiler error occurs here, right? Just adding interfaces doesn't all of a sudden turn inheritance upside-down.
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
O. Ziggy wrote:
Amey Ambulgekar wrote:
O. Ziggy wrote:

Why is the above not allowed? Ball implements Bouncable (Ball IS-A Bouncable) so i thought i should have been able to pass it a Bouncable reference.

Thanks


hello O.Ziggy,

about your program i run on eclipse or even i run through notepad it is not giving me any error it works fine

well check my code if you found any error then tell me ::

interface SampleInterface{

}

class ClassImpl implements SampleInterface {

}

public class InterfaceDemo {

public static void main(String[] args) {

SampleInterface si = new ClassImpl();
fun(si);//<-- your error was here right?

}

public static void fun(SampleInterface si1){

}

}

Thanking you.


no it is not the same.. i am passing a Bounceable to a Ball. You are passing a SampleInterface to a SampleInterface so in your case there is no conflict..


hello O.Ziggy,

i am sorry i did not notice am doing mapping or we can say passing SampleInterface reference to SampleInterface it self you were right

any way we can not do this means passing interface reference to class which implements that interface , so we need here explicit casting that is down casting as Sebanti Sanyal said ...

Thanking you.

(how i forgot that i am passing interface reference to it self... i did not even notice )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing an interface reference to a method expecting an "interface implementor"
 
Similar Threads
Polymorphic behavior provided by inheritance programming assignment
Implementing an interface by changing the argument name
K&B page no 121 doubt
why interface
interface error