• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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){}

}
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Sheriff
Posts: 26783
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic