Friday, November 29, 2013

Java Interface - 4 Reasons Why Should You Use It



The Java Interface is one of the most essential things you should know especially if you want to create large scale system applications. In this post, I list 5 reasons why you should use java interface for your application.

Provides Clearer Code

Java Interface provides us much clearer code. This will help us to increase code readability since you are explicitly defining the function implementations.
public interface EntityLister{

 public void listStudentNames();
 public void listEmployeeNames();

}

public class SchoolAdministrator implements EntityLister {

 public void listStudentNames(){
  System.out.println("Listing student names");
 }
 
 public void listEmployeeNames(){
  System.out.println("Listing employee names");
 }
}
Here, you explicitly defined the functions in Class SchoolAdministrator. You won't feel the need of Java Interface if you're working on a relatively small projects, but I take it on me, code readability is one of the most important things you should consider.

Creates Polymorphism

 

Java Polymorphism is one of the most important concept in Object Oriented Programming. It is the ability of an object to react on different way in just a single and same call. Let's have an example. Suppose we have a parent interface called Car.
public interface Car{
 
 public void move();
 public void stop();
 public void turn();
}
Let's have a class called BMW and Ferrari that extends Class Car.
public class BMW implements Car{

 public void move(){
  System.our.println("moving..");
 }
 
 public void stop(){
  System.out.println("stopping..");
 }
}

public class Ferrari implements Car{

 public void move(){
  System.our.println("moving..");
 }
 
 public void stop(){
  System.out.println("stopping..");
 }
}

Now we can be able to make a call that affects different java objects. Let's create a class that demonstrate it.
public class TestCars{

 public static void main(String args[]){
  Car cars[] = new Car[2];
  cars[0] = new BMW("00-X1");
  cars[1] = new Ferrari("ZZ-43");
  for(Car car : cars){
   car.move();
   car.stop();
  }
 }
}

This is very essential especially if you want your codes to be more organized. Let's try to do the same thing without the use of polymorphism.
public class TestCars{

 public static void main(String args[]){
  BMW bmw = new BMW("");
  Ferrari ferrari = new Ferrari("");
  bmw.move();
  bmw.stop();
  ferrari.move();
  ferrari.stop();
 }
}

Now let's say we have say 200 different java class. Do you still want to use the above method? You would have to manually call every method in 200 java class which is pretty redundant. So java polymorphism promotes code flexibility.

Additional Capabilities

 

I'm not sure if you're going to agree with me if I say Java Interface can add a new feature to class. Let's take a look at my example.
public class FlyingObjects{
 public void fly(){
  System.out.println("flying..");
 }
 
 public void land(){
  System.out.println("landing..");
 }
}

public class Airplane extends FlyingObjects{
 
}
Suppose we just don't want the Airplane to fly but we want it to have a special ability to make itself invisible.
public interface Hideable{
 
 public void hide();
}

public class FlyingObjects{
 public void fly(){
  System.out.println("flying..");
 }
 
 public void land(){
  System.out.println("landing..");
 }
}

public class Airplane extends FlyingObjects implements Hideable{
public void hide(){
  System.out.println("hiding..");
 }
}
Using Java Interface, we add a new method to the Class Airplane without deleting the fact that it was an Airplan that can fly and land.

Multiple Inheritance

 

You can implement as many Java Interface as you can in your Java Class without any errors. This is one of the big advantages of Java Interface. This cannot be done with Java Inheritance. Let's take a look to my example.
public interface Hideable(){
 public void hide();
}

public interface Flyable(){
 public void fly();
}

public interface Shootable(){
 public void shoot();
}

public class Warrior implements Hideable, Flyable, Shootable{
 public void hide(){
  System.out.println("Hiding..");
 }
 
 public void fly(){
  System.out.println("Flying..");
 }
 
 public void shoot(){
  System.out.println("Shooting..");
 }
 
}
This codes will compile without any error. Let's see what will happen if we did not use Java Interface and we want to avail different methods.
public class Hideable(){
 public void hide(){
  System.out.println("Hiding..");
 }
}

public class Flyable(){
 public void fly(){
  System.out.println("Flying..");
 }
}

public class Shootable(){
 public void shoot(){
  System.out.println("Shooting..");
 }
}

public class Warrior extends Hideable, Flyable, Shootable{
 // THIS WILL BE AN ERROR
 
}
This will be a compilation error because you can only extends one class. That's why we should want to use Java Interface if we want to have multiple inheritance that could promote polymorphism.
Thanks for reading my post. :)

No comments:

Post a Comment