Sunday, February 3, 2013

Examples

Interfaces and Inheritance Example:

Interface is like a class, but you can only declare methods and variables in the interface. You cannot implement methods...

I found that this was a standard example of interfaces/inheritance and I wanted to post it here just for my own understanding. It is possible for an interface to inherit from another interface, just like classes can inherit from other classes. You specify inheritance using the extends keyword. 

public interface MySuperInterface {

    public void saiHello();

}
public interface MySubInterface extends MySuperInterface {

    public void sayGoodbye();
}

The interface MySubInterface extends the interface MySuperInterface. That means, that the MySubInterface inherits all field and methods fromMySuperInterface.


Here is an example of an interface that inherits from multiple interfaces:
public interface MySubInterface extends
    SuperInterface1, SuperInterface2 {

    public void sayItAll();
}
As when implementing multiple interfaces, there are no rules for how you handle the situation when multiple interfaces have methods with the same signature (name + parameters). 

No comments:

Post a Comment