Interface

One fine morning, Ms. Sharma comes to my desk to know about the concept of Interface. Now, I started with code example. But she was unable to get that. Now what should I do ?
I started to write a program of a Car which she loves the most...

Now, I am going to tell you guys the whole story.

Suppose, I have to build a car, what should be the most necessary things for a car ?
An Engine, A gear box, a steering, wheels, seats etc etc.

Now you are not the only one who builds a car, Viper, Mazda everyone builds car.

So, WHAT ?
Yes, that's the answer, every car needs to have these basic things. But each one will choose according to their own choice.

Viper is a muscle car so, its acceleration mechanism will differ from a tuning car Mazda. Or simply, your car can be a family car and not a racing car. So, it will have some other features than that of sports car. BUT every car will have engine, wheels, gear, steering.

Do you agree ?
Yeah, without them, a car is nothing.

So basically, we are following some specification to build a car. We must have engine, wheels, gear and steering. But for each model of car, it will be different. Isn't it so ?

Yes.

Well, that's the concept of Interface. When we have some specifications to be followed in each different implementations, we use Interface.

Oracle definition for interface goes as follows, an interface is a group of related methods with empty bodies.
Let's take a code example,


 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public interface Car {  
      void setEngine();  
      void setWheels();  
      void setGear();  
      void setSteering();  
      void drive(int speed);  
 }  

Now here, we see that, we have just declared the methods but no implementation provided.

It means that, if you want to use the interface (to follow the specification of Car), you have to define the methods on your own.

In Java's word, its known as implementing the interface.
Now say, I want to build my own car. So what should I do ?
I will have to implement the Car interface and define the methods with its own body. If we fail to provide implementation of any of them, Compiler will scream on you.

Let's take code example here,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 package blog;  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public class MyCar implements Car {  
      /*  
       * (non-Javadoc)  
       *   
       * @see blog.Car#setWheels()  
       */  
      @Override  
      public void setWheels() {  
           // set 4 wheels  
      }  
      /*  
       * (non-Javadoc)  
       *   
       * @see blog.Car#setGear()  
       */  
      @Override  
      public void setGear() {  
           // set gear  
      }  
      /*  
       * (non-Javadoc)  
       *   
       * @see blog.Car#setSteering()  
       */  
      @Override  
      public void setSteering() {  
           // set steering  
      }  
 }  

This code will never compile. Cause, it does not have the capability to provide the definition of void setEngine(); and void drive(); method.

So, get this working, we need the following,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public class MyCar implements Car {  
      /* (non-Javadoc)  
       * @see blog.Car#setWheels()  
       */  
      @Override  
      public void setWheels() {  
           // set 4 wheels  
      }  
      /* (non-Javadoc)  
       * @see blog.Car#setGear()  
       */  
      @Override  
      public void setGear() {  
           // set gear  
      }  
      /* (non-Javadoc)  
       * @see blog.Car#setSteering()  
       */  
      @Override  
      public void setSteering() {  
           // set steering  
      }  
      /* (non-Javadoc)  
       * @see blog.Car#setEngine()  
       */  
      @Override  
      public void setEngine() {  
           // TODO Auto-generated method stub  
      }  
      /* (non-Javadoc)  
       * @see blog.Car#drive()  
       */  
      @Override  
      public void drive(int speed) {  
           // TODO Auto-generated method stub  
      }  
 }  

Now this looks fine, compiler now knows that you have defined your own implementation for the specification.

If I need to provide my implementations, can't I do that in a class. What was the point to write interface, then implement them in class ?
(This question was raised by Ms. Sharma. Thanks to her.)

Well, now, your car is ready to build. You are well capable of driving your car at 120 Km/hr but your mom in back seat will scream on you that, 'slow down son, slow down, puja thali will be messed up'. Now you really is a fast paced person and really don't wanna drive at slow 40 Km/hr.

Ohh god, what a situation. 
Now, you think of employing Sardarji who nicely drives your car in 40 Km/hr. Now, you are happy and your mom is also happy.
Let's take this in our code,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 package blog;  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 class MyselfAsDriver implements Driver {  
      Car car = new Audi();
      /*  
       * (non-Javadoc)  
       *   
       * @see blog.Driver#driveCar()  
       */  
      @Override  
      public void driveCar() {  
           car.drive(120);  
      }  
 }  
 class DriverSardarji implements Driver {  
      Car car = new MyCar();
      /*  
       * (non-Javadoc)  
       *   
       * @see blog.Driver#driveCar()  
       */  
      @Override  
      public void driveCar() {  
           car.drive(40);  
      }  
 }  
 interface Driver {  
      void driveCar();  
 }  
So, you see that when situations arrive, we can either drive our car on our own or we can employ Sardarji to drive our car.

Now, take a look in our first code example now, we have MyCar, which is capable to be driven by invoking drive(int speed);. Now, your friend builds a car which is even more robust and beautiful, so you want to drive that one. Now think here twice, if you friend have his own class other than drive(int speed), you must have to ask him which method to invoke.

Here lies the beauty of Interface. Implementing the same interface in multiple classes, you are allowed to choose any implementation freely without bothering.
Isn't it so ?

Well, yes. I can choose Audi, BMW, Honda whatever I wish to as long as it has a drive(int speed).
Depending on the situation I can choose the best suited implementation. Or in situations, your mom may choose to have a fast ride with you for a long trip to nearby hill-station with 120 Km/hr or can choose Sardarji to take her to Mandir with her puja thali safe in 40 Km/hr.

Interface lets you define multiple implementation in the same method, which directs to better maintainability. In real life scenario, you may have requirements for persisting data in XML, Database or simple serialized objects (if you are unknown to the terms, please hold your queries back. I promise to come back to you with an answer). So, defining an interface for saving data can be useful because, on situations, you need to use either of the mechanism, where the invoking class will be free of the knowledge underlying implementation. Design Patterns have some good use of it.

Well, in another essence, interface is useful in multiple inheritance, for which, you have to get some knowledge of Deadly Diamond of Death...
There you will be accustomed with multiple inheritance.

Next
Palash Kanti Kundu

No comments:

Post a Comment