Tuesday 23 April 2013

Interfaces in Java

Having learned what are abstract classes and functions we are all set to dig a bit more into polymorphism. Interfaces are based on these two features.

We saw Java does not support multiple inheritance to avoid the problem of Deadly Diamond of Death. To provide functionalities that multiple inheritance support Java provides Interfaces. Interfaces are entities just like classes. We define them one per file and file name must be same as the interface name.

   Synatx for creating an interface is as follows -

    public interface interfaceName{ //code goes here}

Example of an interface is as follows -

public interface Animal{
    public void move();

Couple of things to note in Interfaces
  • All functions in an interface are public and  abstract by default. This means all interface contains is function prototype. It's concrete implementation will be in the class which implements it.Note the semicolon after the function. No implementation.
  • For sub class inheriting from super class we say classA extends classB whereas for interfaces we say classA implements interfaceB.
  • You can define variables in interfaces but they are final and static by default.
  • You cannot create objects of interfaces. You can think them as abstract classes for your understanding because after all it contains abstract functions and we know that non-abstract classes can't have abstract function. only abstract classes can.
 Note : If you have an abstract class will all abstract methods as per convention you should go for interface rather than abstract class (As per Java specs).
Let take a look at any class that may implement interface defined above.

 public class Dog implements Animal{
    public void move() {

    public static void main(String args[]) {
        Animal animal = new Dog();

output Moving..
as expected.

Note in above code we have made use of polymorphism. We are using a Animal reference to refer to a Dog object. All rules of polymorphism will follow.

Note :  We can use interface as polymorphic references in method arguments and return types. Meaning you can have a method that return Object of type Animal. At runtime you can return any class Object that implements Animal interface.

Why are the advantages of using interfaces?

  • A class can implement multiple interfaces but can extend only one class.
  • Interfaces can be used across inheritance trees. This means a class from one inheritance tree can implement interface from another inheritance tree.
  • Advantages of polymorphism follows.
  • In later parts of tutorials on Java you will realize generics , design pattens etc depend on this.

One of the OOP principle is "Code for the interface not for implementation" meaning always thing how to design your interface then to worry about your implementation.

Note :  If you have abstract class with all abstract methods (no non-abstract methods) it is better to go for an interface.

No comments:

Post a Comment

t> UA-39527780-1 back to top