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
Copy package com . gs . ilp . corejava . interfaces ;
public interface Interviewer {
public void conductInterview ();
}
Copy package com . gs . ilp . corejava . interfaces ;
public interface Trainable {
public void attendTraining ();
}
Copy package com . gs . ilp . corejava . interfaces ;
public class Employeeee {
String name;
String address;
String phoneNumber;
String experience;
}
Copy 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" );
}
}
Copy 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" );
}
}
Copy 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 ();
}
}
Types of methods in an interface :
Rules to implement Interface:
A class can implement multiple interfaces:
Multiple inheritance is possible using Interfaces:
Properties of a member of an interface: