print
Advertisment
Advertisment

Interfaces in java

The interface is a predefined word (reserved word) in Java. Which interface is the blueprint of a class in Java, It is like class, the interface has methods and variables but the interface has declared methods, abstract. In Java, it is used to achieve abstraction and inheritance. It has all the methods- abstract and public and all the fields- public, static and final. Abstract methods declared in the interface need to be implicit in the implementing class.

  • Interfaces have an abstract method but they do not have a method body.
  • It presents an IS-A relationship.
  • We cannot create an instance of an interface but we can create a reference to it, which refers to the object of the implemented class.
  • Since Java 8, interfaces can also have default and static methods.
  • Since java 9, it can also have private methods.
  • A class cannot implement two interfaces whose methods have the same name but the return type is different.

How is an interface declared?

An interface is declared by the interface keyword. It provides complete abstraction which means that all the methods in the interface are declared with empty body.

Syntax

    
  interface interface_name {  
      // fields
      // abstract/private/default methods
    }
 
  

Advantage of interface

  1. It is used to achieve complete abstraction.
  2. By this, we can achieve multiple inheritances. Because a Java class can implement multiple interfaces.
  3. It is also used to achieve loose coupling.
  4. An interface can extend more than one interface.

Disadvantage of interface

  1. we cannot create an instance of interface.
  2. We can never make the interface private & protected.
  3. In this, at the time of declaration, it is necessary to initialize the variable otherwise the compiler will throw an error.
  4. One interface cannot implement another interface.

Interface implementation –

The implements keyword is used to implement the interface in java.
Example of this In the example given below, we have created an interface named Color and implemented it through Redcolor class.

Example

    
public interface Color {
	public void blue();
	public void green();
}

class ColorImpl implements Color{
	public void blue(){
		System.out.println("this is blue color");
	}

	public void green(){
		System.out.println("this is blue color");
	}
	public static void main(String[] args) {
		ColorImpl a = new ColorImpl();
		a.blue();
		a.green();
	}
}
	
  

Output :

 
  
this is blue color
this is blue color
    
Advertisment

Can we make two methods in the same interface with the same method name and same parameter?

It is not allowed that we can create two methods with the same method name & same parameters in the same interface. And it will show a compile-time error.

Example


public  interface Interface_example1{
	public void money();
	public void money();

}

class Interface_example1Impl implements Interface_example1{
	public void money(){
		System.out.println("Interface Method Implemented");
	}
	public static void main(String[] args) {
		Interface_example1Impl obj = new Interface_example1Impl();
		obj.money();
	}
}

Output :

 
  
Factorial.java:1: error: class Interface_example1 is public, should be declared in a file named Interface_example1.java
public  interface Interface_example1{
        ^
Factorial.java:3: error: method money() is already defined in interface Interface_example1
        public void money();
                    ^
2 errors
    

Implementing more than one interface in a class

Example

    
 
 public  interface Company {
     public void money();

      }
  public  interface Employee{
       public void worker();
      }
  class Program implements Company,Employee{

  public void money(){
    System.out.println("10000 salary");
  }

  public void worker(){
      System.out.println("100 workers");
    }
  
  public static void main(String[] args) {
    
   Program p = new Program();
    p.money();
    p.worker();
  }
     
}


  

Output :

 
  
 10000 salary
 100 workers
    
Advertisment

Method Signature in java

In Java, a method signature is part of the method declaration. It's consists of the method name and the parameter list. Method signature does not include the return type of the method. A class cannot have two methods with the same signature. If we try to declare two methods with the same signature you will get a compile-time error.

Syntax

    
     void method(passed argument) {
        // Statement 1
        //Statement 2
       }

  

Example

    
class Signature{
	static int date;
	static String month;
	static String year; 
	void method(int x,String y,String z) {
		this.date = x;
		this.month = y;
		this.year  = z;
	}

	public static void main(String[] args) {
		Signature s = new Signature();
		s.method(19,"Sept","2021");
		System.out.println(date+" "+month+" "+year);
	}
}

  

Output :

 
  
  19 Sept 2021
    
Advertisment
Advertisment
arrow_upward