Java Interface part2



java interface

In the previous java interface tutorial, I explained what is an interface and what does it contain. Then I provided a step by step example. Everything went smooth until I decided to add more abstract methods to my Smartphones interface which surly resulted in application crash and errors. If you missed the previous tutorial, I suggest you read it first since there will be a straight relationship between this tutorial and the previous one. The interface tutorial can be found here .

Once again, I would like to remind you with java interface general syntax. Here it is.

public interface interface-name {
// if any data are defined, they must be constants
public static final type-name var-name = constant-expr;
// one or more implicitly abstract and public methods
return-type method-name ( formal-params );
}

Here is the scenario: We added some abstract methods to Smartphones interface in order to handle new added features to smartphones such as an integrated barcode scanner, an integrated RFID reader and even a spying application. Once we added those abstract methods, the application stop working and throwing errors. Why? Because the added abstract methods to the Smartphones interface must be implemented by all the subclasses that inherit from it (imagine the subclasses are huge, it will be painful to do the change). Thus, those added abstract methods were not implemented and this will result in program or application crash unless all the clients implement them. In this case, we are forcing the clients to implement these abstract methods. The move is certainly not very professional. So, what is the solution?

The solution is to create another interface that extends or inherits form Smartphones interface. In this way, the clients have the choice to upgrade their applications by using the newly created interface or the clients continue to use the old version and keep their applications unchanged.
To extend an interface, use the keyword extends.

I created a new interface named Newfeatures that extends or inherits from the Smartphones interface. Now, the clients who want to upgrade to the new version, they must implement the added abstract methods by implementing the Newfeatures interface instead of Smartphones interface. If you missed the previous tutorial about the interface, maybe you are wondering what does the Smartphones interface look like. Here it is.

public interface Smartphones {
// Field declaration must be public static final that means constant
public static final String message = "Smartophone specification:";
// Abstract methods, they have to be implemented in concrete class
//also known as subclass
void displayMessage();
void PhoneMaker ();
void phoneSpecs();
}

Note the absence of the modifier public and the keyword abstract in the Smartphones interface. They are not needed since all methods inside an interface are implicitly public and abstract. However, their presence will not affect your program and it will work correctly, but why typing them since the interface does the job.

The interface Smartphones has one data field declared as constant and three abstract methods. The abstract methods have to be implemented by any subclass that inherits from it or make use of it. If the subclass fails to do so it has to declare itself as an abstract class. To implement the abstract methods of an interface in a subclass, use the keyword implements.

The Newfatures interface is shown below.

public interface Newfeatures extends Smartphones {
void hasIntegratedScanner(String YesNo);
void hasSpying(String answer);
}

The rest of the code will be based on the Newfeatures interface. If you want to see clearly the difference between how the application was created using Smartphones interface and how it's created using the Newfeatures interface, then read first the previous tutorial about interface. Now, it's time to implement Android class, Iphone class and the main class Testall using Newfeatures interface.

Let's see our first subclass named Android that implements the abstract methods of Newfeatures interface to meet its specific needs.

public class Android implements Newfeatures {
String makers = null;
String osVersion = null;
String screenResolution = null;
String serialNumber = null;
String model = null;

public void displayMessage()
{ // message is a public constant declared in Smartphones interface System.out.println(message);
}

public void PhoneMaker(){
makers = "Samsung";
System.out.println("Phone Maker: " + makers);
}

public void phoneSpecs(){
osVersion = "Android 4.2 Jelly Bean";
screenResolution = "1920 x 1080-pixel";
serialNumber = "123456789";
model = "Galaxy S4";
System.out.println("OS Version: " + osVersion);
System.out.println("Screen Resolution: " + screenResolution);
System.out.println("Serial Number: " + serialNumber);
System.out.println("Phone Model: " + model);
}

public void hasIntegratedScanner(String YesNo){
if(YesNo.equals("Yes"))
{
System.out.println("Itegrated Scanner: Yes");
}
else { System.out.println("Itegrated Scanner: No");
}
}

public void hasSpying(String answer){
if(answer.equals("Yes")){
System.out.println("Has Spy Program: Yes");
}
else{ System.out.println("Has Spy Program: No");
}
}
}

The same abstract methods of the interface Newfeatures can be implemented differently by Apple for its iPhone brand to meet its specific needs. Below is the Iphone class that implements the abstract methods of Newfeatures interface.

public class Iphone implements Newfeatures {
String makers = null;
String osVersion = null;
String screenResolution = null;
String serialNumber = null;
String model = null;

public void displayMessage()
{ // message is a public constant declared in Smartphones interface System.out.println(message);
}

public void PhoneMaker(){
makers = "Apple";
System.out.println("Phone Maker: " + makers);
}

public void phoneSpecs(){
osVersion = "iOS 7";
screenResolution = "1136 x 640 -pixel";
serialNumber = "9009997865";
model = "iPhone 7S";
System.out.println("OS Version: " + osVersion);
System.out.println("Screen Resolution: " + screenResolution);
System.out.println("Serial Number: " + serialNumber);
System.out.println("Phone Model: " + model);
}

public void hasIntegratedScanner(String YesNo){
if(YesNo.equals("Yes"))
{
System.out.println("Itegrated Scanner: Yes");
}
else { System.out.println("Itegrated Scanner: No");
}
}

public void hasSpying(String answer){
if(answer.equals("Yes")){
System.out.println("Has Spy Program: Yes");
}
else{
System.out.println("Has Spy Program: No");
}
}
}

At this point we have an interface named Newfeatures, that inherits from Smartphones interface, and two subclass that inherit from it named Android and Iphone.

To test their functionality, you must create an entry point class which is a class with main method that enables java to execute your program. I named the class Testall.

To successfully perform a test do the following.

First create an interface file and name it Smarptphones.java and copy and paste the code of Smartphones interface. Second, create an interface file and name it Newfeatures.java

and copy and paste the code of Newfeatures interface. Then create two class files named respectively Android.java and Iphone.java and copy and paste the corresponding code to each class. Finally, create the class file and name it Tesall.java then copy and paste the code of Testall class. I would like to add that copy and paste is not a good way to learn. I suggest that you type the code. Without being too verbose, here is the main class Testall implementation.

public class Testall {
public static void main(String[] args) {
Android myPhone = new Android();//Instance of Android class
myPhone.displayMessage();
myPhone.PhoneMaker();
myPhone.phoneSpecs();
myPhone.hasIntegratedScanner("Yes");
myPhone.hasSpying("Yes");
System.out.println("========================== iPhone Specs =============");
Iphone myiPhone = new Iphone();//Instance of iPhone class
myiPhone.displayMessage();
myiPhone.PhoneMaker();
myiPhone.phoneSpecs();
myiPhone.hasIntegratedScanner("Yes");
myiPhone.hasSpying("Yes");
}
}

I hope this post helped you.