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
That's the reason why interface are needed
Code:
package com.gs.ilp.corejava.interfaces;
public interface Interviewer {
public void conductInterview();
}
package com.gs.ilp.corejava.interfaces;
public interface Trainable {
public void attendTraining();
}
package com.gs.ilp.corejava.interfaces;
public class Employeeee {
String name;
String address;
String phoneNumber;
String experience;
}
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");
}
}
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");
}
}
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:
UML :
Defining Interfaces:
Types of methods in an interface :
Rules to implement Interface:
A class can implement multiple interfaces:
Rule 1:
Rule 2:
Multiple inheritance is possible using Interfaces: