> For the complete documentation index, see [llms.txt](https://gyansetu-java.gitbook.io/core-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-java.gitbook.io/core-java/interfaces.md).

# 24) Interfaces

### Need of Interfaces:

![](/files/-LU8xCELPRnZfkDJuFrs)

![](/files/-LU8xGmPWMtS9pxAqrTM)

![](/files/-LU9vY4YsUA3BsiMhlqQ)

### Example:

Suppose we have two classes Programmer and Manager both of them extend some common feature from Employee class.Now let say we have the below requirement/behavior to implement

![](/files/-LU9wS4a1-EGDPvc3CqO)

![](/files/-LU9xRI7DSHoCwrTptJv)

### That's the reason why interface are needed

![](/files/-LU9xkwj8Oa1kzr6i4TG)

![](/files/-LU9xw9sBOPDZFEUUSb1)

![](/files/-LU9y3QHoT7qUK6QSA75)

![](/files/-LU9yDJMko3hSyX9jKzd)

#### Code:

```java
package com.gs.ilp.corejava.interfaces;

public interface Interviewer {
	public void conductInterview();
}
```

```java
package com.gs.ilp.corejava.interfaces;

public interface Trainable {
	public void attendTraining();
}
```

```java
package com.gs.ilp.corejava.interfaces;

public class Employeeee {
	String name;
	String address;
	String phoneNumber;
	String experience;
}
```

```java
package com.gs.ilp.corejava.interfaces;

public class Manager extends Employeeee implements Interviewer, Trainable {
	int teamSize;
	void reportProjectStatus() {
	}
	public void attendTraining() {
		System.out.println("Mgr - attendTraining");
	}
	public void conductInterview() {
		System.out.println("Mgr - conductInterview");
	}
}
```

```java
package com.gs.ilp.corejava.interfaces;

public class Programmer extends Employeeee implements Trainable {
	String[] programmingLanguages;
	void writeCode() {
	}
	public void attendTraining() {
		System.out.println("Prog - attendTraining");
	}
}
```

```java
package com.gs.ilp.corejava.interfaces;

public class TestInterfaces {
	public static void main(String[] args) {
		Manager manager = new Manager();
		manager.attendTraining();
		manager.conductInterview();

		Programmer programmer = new Programmer();
		programmer.attendTraining();
	}
}
```

#### Output:

![](/files/-LawneOZTPLhBTd9Sw6T)

#### UML :

![](/files/-LU9ySZ8Q-kp4ufMmdSt)

### Defining Interfaces:

![](/files/-LU9zEBMn061VJ9qBkYj)

![](/files/-LU9zNZiGWOhtZ9ZS8Pq)

![](/files/-LU9zXWHmgcKIMwnvxce)

![](/files/-LU9zdnd_CzOoEvos8EW)

![](/files/-LU9zjwyD4NtKeNqX1Gc)

![](/files/-LU9zuRdGYB3HW2BqhWJ)

![](/files/-LUA-2Qt8cI1P8NxDR_I)

![](/files/-LUA-9GkYGqS6KX_Cx9x)

### Types of methods in an interface :

![](/files/-LUA-bv3aDsqgWF1D_-f)

![](/files/-LUA-hiT0krVh0b4Omf0)

![](/files/-LUA-nUnW9lT1v8EKvIo)

### Rules to implement Interface:

![](/files/-LUA8G2Y-TxDIZlYg_4D)

![](/files/-LUA8NqXvlGDLwgWzf9l)

### A class can implement multiple interfaces:

#### Rule 1:

![](/files/-LUAAfG0JjGr_tVch42l)

![](/files/-LUAAjErurwm14ao1tgq)

![](/files/-LUAB02iPzXEydhWJsFO)

![](/files/-LUAB4xX33280Qow7wEr)

#### Rule 2:

![](/files/-LUADFenNMC-87LYlFwu)

![](/files/-LUADLn2aVsc-rgTwHB6)

### Multiple inheritance is possible using Interfaces:

![](/files/-LUADvwUypd8KnrMc-8C)

![](/files/-LUAE44fJ7l9P5TZjxxX)

![](/files/-LUAEKbXIzSylYYjmI2a)

### Properties of a member of an interface:

![](/files/-LUAEu1F7TTUnnC9Nsmi)

![](/files/-LUAEytCC38XoXDSCKDC)
