Java Interface part1



java interface

An interface defines a standard and public way of specifying the behavior of classes that implement it. Unlike the abstract class that mixes data fields (variables), non-abstract methods and abstract methods, an interface contains only abstract methods and data fields constants. In other words, the interface contains methods headers or signatures (methods without implementation) and it may contain field definitions which must be a class constants only. That means any interface data field must be declared public, static, final only. Like an abstract class, an interface is never instantiated directly. So, you can think of an interface as an abstract class with all abstract methods and data fields constant if there are any. Thus, All methods declared in an interface are implicitly abstract and implicitly public. It is not necessary, and in fact considered redundant to declare a method in an interface to be abstract. The following is a general syntax of a java interface.

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 );
}

I will stick with same example I used for abstract class. This will provide you with a clear idea how to make transition from an abstract class to an interface.
Let's start with Smartphones interface. 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. Let's see our first subclass named Android that implements the abstract methods of Smartphones interface to meet its specific needs.

public class Android implements Smartphones {
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);
}

The same abstract methods of the interface Smartphones can be implemented differently for example by Apple for its iPhone brand to meet its specific needs. Below is the Iphone class that implements the abstract methods of Smartphones 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);
}

At this point we have an interface named Smartphones 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 as shown below.

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

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. Then create two other 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.

Now, you should feel comfortable with the interface. But wait a second, what will happen if you decide to add more abstract methods to the interface Smartphones, known that the interface Smartphones is implemented by different clients. Sure, everything will crash and stop working. So what is the solution? Here is the solution.