Using interfaces with a traffic light system

Consider the typical traffic light system, which is used everywhere around the world to maintain traffic regulations. Every country has its own traffic rules, such as driving on the left or right side of the road. Even though traffic rules differ from country to country, there are certain rules that apply globally and need to be followed by every country. One such rule is the use of traffic lights to govern the traffic flow, where a red light is an indication to stop, an amber/yellow light to ready your engine, and a green light to move your vehicle. Let's say that these global rules are imposed by a central traffic authority, and we want to implement, for example, the Australian traffic system. This system will have its own rules, but we need to make sure that it follows the global rules imposed by the central traffic authority.

Using this example, we'll try to understand the concept of interfaces. Here, the central traffic authority acts as an interface and the Australian traffic rules act as a class that implements the interface; that is, the Australian traffic system will have to follow the rules/methods mentioned in the central traffic authority interface. The methods defined in any interface are just signatures, so the classes will define and implement the methods present in an interface. Let's look at this example in our Java code.

We define an interface in the same way that we define a class. In this traffic light example, let's name the class as CentralTraffic. We now have a ready interface, as follows:

package demopack;

public interface CentralTraffic {

public void greenGo();
public void redStop();
public void FlashYellow();

}

We can see in the syntax that instead of class we have written interface. We define a method in the interface using the same method that we use to define a method in a class, but remember that we cannot have a method body defining the method as this is an interface, and doing so will throw an error. Create another class to implement this interface and name it AustralianTraffic. Once we have a Java class, we need to implement the CentralTraffic interface to it, and we do that using the implements keyword, as follows:

public class AustralianTraffic implements CentralTraffic {

After using the preceding sentence, our IDE will show an error, and when you hover over the error, you'll see some suggestions related to the error. One suggestion would be to import CentralTraffic, and another would be to add unimplemented methods. Click on these suggestions to resolve the error and you should end up with the following code:

package coreJava;
import demopack.CentralTraffic;
public class AustralianTraffic implements CentralTraffic {

public static void main(String[] args) {

}
@Override
public void greenGo() {
// TODO Auto-generated method stub
System.out.println(" greengo implementation")
}
@Override
public void redStop() {
// TODO Auto-generated method stub
System.out.println(" redstop implementation")
}
@Override
public void FlashingYellow() {
// TODO Auto-generated method stub
System.out.println(" flash yellow implementation")
}

}

All the methods defined in the CentralTraffic interface can be seen in the AustralianTraffic class, and here we can also implement these methods as we wish. Now, if we remove the greenGo method from our Java class, it'll give us an error. As it is a method defined in an interface, it is mandatory for us to implement all the methods defined in the interface.

The interface methods are defined outside public static void main, and to execute these methods, we should create a class object for them in the main method, as follows:

        CentralTraffic a= new AustralianTraffic();

This line of code says that we have created an object for the AustralianTraffic class to implement the methods present in the CentralTraffic interface. The main class should look as follows:

public class AustralianTraffic implements CentralTraffic {

public static void main(String[] args) {
CentralTraffic a= new AustralianTraffic();
a.redStop();
a.FlashYellow();
a.greenGo();
}

Now, after implementing the methods from the interface, we can define our own country-specific methods (rules) in our Java class, as follows:

public void walkonsymbol()
{
System.out.println("walking");
}

In our main method, if we try calling our country-specific method using a., like we did for the other methods in the main class, then we will find that we won't be able to do so because the walkonsymbol method is specific to a particular country (that is, the AustralianTraffic class) and it's not implemented in CentralTraffic. For the walkonsymbol method, we need to create another object in the main class specific to the AustralianTraffic class, as follows:

        AustralianTraffic at=new AustralianTraffic();
at.walkonsymbol();

Another piece of information related to the interface is that a class can implement more than one interface. Let's say that we create another interface, such as ContinentalTraffic, and define another rule related to traffic lights, such as a train symbol to indicate that a train is passing by. We can implement this interface in our AustralianTraffic class simply by adding a comma, as follows:

public class AustralianTraffic implements CentralTraffic, ContinentalTraffic {

For this interface, we need to follow the same steps as we did for the CentralTraffic interface, such as importing ContinentalTraffic to AustralianTraffic, adding unimplemented methods, creating an object specific to ContinentalTraffic in the main class, and so on.

Now you have a fair idea of the differences between an interface and a class. We learned how to define the interfaces, how to implement them within another class, and how to call them using objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.104.27