Java

Interfaces in Java

Programmingempire

Interfaces in Java provide us a way to declare the functionality that can be implemented by different classes in their own way. In general, an interface represents a contract. Basically, it is a contract between different groups of people in the software industry where the groups want to agree on how the software should behave. Therefore, the interface contains information regarding what functionality should be available in the software. However, the implementation of the functionality is left with specific vendors. So, a particular vendor implementing an interface in their software is free to implement functionality in their own way.

Using Interfaces in Java

The following syntax is used to define an interface in Java. Once, we define an interface, it can be implemented by one or more classes using the implements keyword.

// Defining an interface
interface interface-name
{
    // interface-members
}

//implementing interface
class class-name implements interface-1, inteface-2, ...
{
    // class members
}

Important Points Regarding Interfaces in Java

  • In fact, interfaces provide a mechanism to specify abstraction in Java.
  • Since Java doesn’t allow a class to have more than one parent class, interfaces provide a way to achieve multiple inheritance
  • A class can implement more than one interface
  • Whenever a class implements an interface, it must define all methods declared in that interface
  • An interface can have only abstract methods. Therefore, methods are declared only. While method body is not provided.
  • Also, all fields in the interface are public, static, and final by default.
  • Whenever a class is inherited from another class as well as implement interfaces, then the interface list should appear after the class is inherited using the extends keyword. The following example demonstrates it.
    interface I1
    {
    	
    }
    interface I2
    {
    	
    }
    class A
    {
    	
    }
    class B extends A implements I1, I2{ 
    	
    }

As can be seen, there are two interfaces I1, and I2. Also, there are two classes A, and B respectively. Further, suppose, class wants to inherit from class A, as well as implements the two interfaces I1, and I2. Therefore it is required that the inheritance should be specified before the class implements interfaces.

Example of Using Interfaces in Java

The following code shows an example of using interfaces in Java. As shown below, the interface name my_interface contains two fields and a declaration for the method Compute(). Further, there are two classes implementing this interface and providing their own definition of the method Compute(). Furthermore, the method Compute() is called using the reference variable of the interface which refers to the object of the specific class in different function calls..

package InterfaceExamples;
public class InterfaceDemo {
	public static void main(String[] args) {
		my_interface r;
		r=new FirstClass();
		System.out.println("Value Computed by FirstClass method: "+r.Compute());
		r=new SecondClass();
		System.out.println("Value Computed by SecondClass method: "+r.Compute());

	}
}
    interface my_interface
    {
    	int p=7, q=16;
    	int Compute();
    }
    class FirstClass implements my_interface
    {
    	public int Compute()
    	{
    		return p*p + q*q;
    	}
    }
    class SecondClass implements my_interface
    {
    	public int Compute()
    	{
    		return p*p - q*q;
    	}
    }

Output

Example of Interfaces in Java
Example of Interfaces in Java

programmingempire

You may also like...