Home Application Develop Complete Guide Interface in Java

Complete Guide Interface in Java

3
0

Today I have shared Interface in Java Java does not support multiple inheritances. However, there are several cases when it becomes mandatory for an object to inherit properties from multiple classes to avoid redundancy and complexity in the code. For this purpose, Java provides a workaround in the form of interfaces. Also, Java provides the concept of nested classes to make certain types of programs easy to manage, more secure, and less complex.

An Interface in Java is a contract that specifies the standards to be followed by the types that implement it. The classes that accept the contract must abide by it. An Interface in Java and a class are similar in the following ways:

An Interface Can Contain Multiple Methods.An Interface in Java is saved with a .java extension and the name of the file must match with the name of the (I-F) just as a Java class.The bytecode of an Interface is also saved in a .class file.Interfaces are stored in packages and the bytecode file is stored in a directory structure that matches the package name.

Interface-in-Java

An Interface in Java and a class differ in several ways as follows:

  • An interface (I-F) cannot be instantiated.
  • An (I-F) cannot have constructors.
  • All the methods of an (I-F) are implicitly abstract.
  • The fields declared in an (I-F) must be both static and final.
  • The (I-F) cannot have instance fields.
  • An (I-F) is not extended but implemented by a class.
  • An (I-F) can extend multiple interfaces.

In several situations, it becomes necessary for different groups of developers to agree to a ‘contract’ that specifies how their software interacts. Each group should have the liberty to write their code in their desired manner without having the knowledge of how the other groups are writing their code.

Interfaces in Java can be used for defining such contracts. (I-F’s) do not belong to any class hierarchy, even though they work in conjunction with classes.  Java does not permit multiple inheritances for which (I-F) provide an alternative.  

In Java, a class can inherit from only one class but it can implement multiple (I-F).Therefore, objects of a class can have multiple types such as the type of their own class as well as the types of the (I-F) that the class implements.

Interface in Java Below Scripts 

The syntax for declaring an (I-F) is as follows:

Syntax

<visibility> interface <(I-F)-name> extends <other-(I-F), … >

{

// declare constants

// declare abstract methods

}

where,

<visibility>: Indicates the access rights to the (I-F). Visibility of an (I-F) is always public.

<I-F-name>: Indicates the name of the I-F.

<other-I-F>: List of I-F that the current I-F inherits from.

For example,

public interface Sample extends Interface1{

static final int someInteger;

public void someMethod();

}

In Java, I-F  names are written in CamelCase, that is, the first letter of each word is capitalized. Also, the name of the I-F describes an operation that a class can perform. For example,

  • interface Enumerable
  • interface Comparable

Some programmes prefix the letter ‘I’ with the I-F name to distinguish I-F’s from classes. For example,

  • interface IEnumerable
  • interface Icomparable

Notice that the method declaration does not have any braces and is terminated with a semicolon. Also, the body of the I-F contains only abstract methods and no concrete method. Since all methods in an I-F are implicitly abstract, the abstract keyword is not explicitly specified in the method signature.

A dialog box appears where the user must provide a name for the I-F and then, click OK. This will create an I-F with the specified name. Following code snippet defines the I-F, IVehicle:

public interface IVehicle {

// Declare and initialize constant

static final String STATEID=”LA-09”; // variable to store state ID

/**

 * Abstract method to start a vehicle

 * @return void

 */

public void start();

/**

 * Abstract method to accelerate a vehicle

 * @param speed an integer variable storing speed

 * @return void

 */

public void accelerate(int speed);

/**

 * Abstract method to apply a brake

 * @return void

 */

public void brake();

/**

 * Abstract method to stop a vehicle

 * @return void

 */

public void stop();

}

The Syntax to Implement an Interface in Java is as follows:

class <class-name> implements <I-F 1>,…

{

// class members

// overridden abstract methods of the I-F(s)

}

Following code snippet defines the class TwoWheeler that implements the IVehicle I-F:

class TwoWheeler implements IVehicle {

String ID; // variable to store vehicle ID

String type; // variable to store vehicle type

/**

* Parameterized constructor to initialize values based on user input

* @param ID a String variable storing vehicle ID

* @param type a String variable storing vehicle type

*/

public TwoWheeler(String ID, String type){

this.ID = ID;

this.type = type;

}

/**

 * Overridden method, starts a vehicle

* @return void

 */

@Override

public void start() {

System.out.println(“Starting the “+ type);

}

/**

 * Overridden method, accelerates a vehicle

 * @param speed an integer storing the speed

 * @return void

 */

@Override

public void accelerate(int speed) {

System.out.println(“Accelerating at speed:”+speed+ “ kmph”);

}

/**

 * Overridden method, applies brake to a vehicle

 * @return void

*/

@Override

public void brake() {

System.out.println(“Applying brakes”);

}

/**

 * Overridden method, stops a vehicle

 * @return void

 */

@Override

public void stop() {

System.out.println(“Stopping the “+ type);

}

/**

 * Displays vehicle details

 * @return void

 */

public void displayDetails(){

System.out.println(“Vehicle No.: “+ STATEID+ “ “+ ID);

System.out.println(“Vehicle Type.: “+ type);

}

}

/**

 * Define the class TestVehicle.java

 */

public class TestVehicle {

/**

 * @param args the command line arguments

 */

public static void main(String[] args){

// Verify the number of command line arguments

if(args.length==3) {

// Instantiate the TwoWheeler class

TwoWheeler objBike = new TwoWheeler(args[0], args[1]);

// Invoke the class methods

objBike.displayDetails();

objBike.start();

objBike.accelerate(Integer.parseInt(args[2]));

objBike.brake();

objBike.stop();

}

else {

System.out.println(“Usage: java TwoWheeler <ID> <Type> <Speed>”);

}

}

}

Following figure shows the output of the code when the user passes CS-2723 Bike 80 as command line arguments:

Output-Interface-in-Java

The Above Screenshot is display out put, that’s complete guide about Interface in Java, I hope you understood as well, If you like this tutorial/article, kindly share with personal friends on social networks and subscribe my website social network pages like Facebook, Twitter, Google+, YouTube Channel and Email Address for post updates. If you have any question / suggestion about this article/post kindly comment or email me after received your email or comment, I will reply with your answer about 24 Hours. Thanks for Read and visit my Website, keep touch my website.

Subscribe My YouTube Channel For Get Updates 🙂