doubt regarding interfaces
hi,
i have a doubt abt interfaces...when a class is implementing an interface we say
public class someclass implements someinterface
but where as if an interface is implementing another interface we say
public interface interface1 extends interface2 rather, public interface interface1 implements interface2.
what might be the reason for this..................
We can not create an instance for an interface, but a class can do. so, class implements the interface.
Since interface can include other interface methods, it can extend an other interface.
In short, when u implement something, it should be able to create an object/instance. So, interface can only extend other interface.
Am i clear....
Srinivas
It's simpler than that, even.
This interface declares the method foo. It does not implement it.
public interface Foo {
public void foo();
}
This class implements and declares the method foo, and thus it implements the methods required by the Foo interface.
public class Fooable implements Foo {
public void foo() {
System.out.println("The implementation");
}
}
This interface does not implement anything from the parent interface. It just builds upon (extends) the methods in the parent interface.
public interface Bar extends Foo {
public void bar();
}
In other words, those keywords describe exactly what's going on. Something implements something else if it provides the implementation. Something extends something else if it merely provides an extension to what is already implemented or declared.
Dave.
Good example Dave....
You explained what the question is about with an example, but yusuf question is that "Why an interface can't implement another interface?"
So, answer is, if interface has to implement other interface then subinterface has to define a body(code) to all the methods of parent interface which is controversy to the definition of an interface.
what do u say?
Srinivas
This question is for Mr.Srinivas
as u said....
"In short, when u implement something, it should be able to create an object/instance. So, interface can only extend other interface."
...i want to create an abstract class in which i want to add the functions of an interface like..
abstract public class A implements B
{
public void fun(){}
public void f();
}
public interface B
{
public void someMethod();
}
i dont want to provide body for the method called "someMethod" then as per ur explaination i have to write the keyword "extends" as i cant create an object of abstract class
eg: abstract public class A extends B
{
}
but i am not even able to compile this program.....
could u plz make it clear .......
When u say "abstract public class A implements B", the method someMethod() must be defined in class A.May i know the exception it is throwing on compliation.Srinivas