Need Help.

Hi Friends,I have a question, can I call the method from interface ?If it is then how it works I mean how it invokes the method from interface.Waiting for your reply.Thanks in advance.Regards - Pravin
[249 byte] By [pravinmka] at [2007-11-27 4:58:39]
# 1
> Hi Friends,> > I have a question, can I call the method from> interface ?> ...Have you tried it?
prometheuzza at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 2
no but upto my knowledge we can not call the method from interface.
pravinmka at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 3
> no but upto my knowledge we can not call the method> from interface.Err, I don't understand. Why don't you try it?
prometheuzza at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 4

First, how to invoke method from interface?

you must first create an instance to invoke its member method.

then can you create an instance from any interface?

You have to implement your interface first by a concrete class, then create an instance from the implementation class, like that:

interface MyInterface

{

public void someMethod();

}

class MyClass implements MyInterface

{

public void someMethod()

{

// do something

}

}

then create your instance using:

MyClass obj = new MyClass();

or

MyIterface obj = new MyClass();

then you can invoke the interface method:

obj.someMethod();

really you call the class method not interface one, even at creating your instance by : MyIterface obj .......

Good Luck

Ahmad Elsafty

NourElsaftya at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 5

> First, how to invoke method from interface?

> ...

I think the OP meant if this is possible or not:

interface Foo {

void bar() { baz(); }

void baz();

}

And YOU don't need to answer the above for all I'm concerned. I'd rather have it the OP would try it him/herself first.

prometheuzza at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 6

try to compile your code:

interface Foo {

void bar() { baz(); }

void baz();

}

you will get a compile error message sayes that Interface methods can not contain body.

NourElsaftya at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 7
> try to compile your code:> ...> you will get a compile error message sayes that> Interface methods can not contain body.*sigh*
prometheuzza at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 8
> > try to compile your code:> > ...> > you will get a compile error message sayes that> > Interface methods can not contain body.> > *sigh*You didn't expect him to actually read your post, did you?
macrules2a at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 9
> You didn't expect him to actually read your post, did> you?Well, he did use the code from his post.
CaptainMorgan08a at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...
# 10
> > *sigh*> > You didn't expect him to actually read your post, did you?Reading the entire post would indeed be a bit of a stretch...
prometheuzza at 2007-7-12 10:14:37 > top of Java-index,Java Essentials,Java Programming...