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
> 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.
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.