Interfaces: =========== ==> Interface acts like contract (rules/guidelines) for a set of classes to provide implementation. ==> Abstraction: abstract classes Interface ==> Interface is other way/option to implement an abstraction. ==> To define interface, we need to use "interface" keyword Syntax: interface InterfaceName{ } ==> In general, the interface consisting of members like: 1) Public static final variables 2) Public abstract methods Syntax: interface MyInterface{ public static final a; public abstract void m1(); } Note: ===== 1) By default an interface should be either default or public type. 2) The data members without public static final in the interface can automatically treated/understood as "public static final" type. 3) methods of interface can public abstract methods automatically. Q: Do we allowed to define concrete methods in interface? ========================================================== No // default interface interface MyInterface{ int x = 100; // automatically this can understand as public static final type void m1(); // automatically understand as public abstract method. public void m2() {// error System.out.println("Hi"); } } ====================================== Q: DO we able to create an object for an interface? ==================================================== No. Because the interface cannot be allowed for the constructor definition. So, we cannot able to create an object without the constructor. ==> class can be implements from an interface using "implements" keyword. Syntax: class MyClass implements InterfaceName{ } package AIT.IO.Pack.Java; // default interface interface MyInterface{ int x = 100; // automatically this can understand as public static final type void m1(); // automatically understand as public abstract method. } class MyClass implements MyInterface{ @Override public void m1() { System.out.println("Hi"); } } public class MainClass { public static void main(String[] args) { MyClass mc = new MyClass(); // MyInterface mi = new MyInterface(); MyInterface mi = new MyClass(); } } Note: ===== We can create an object for the implemented class with interface reference. ==> We can extend one interface to another interface. Ex: interface MyInter1{ void m1(); } interface MyInter2 extends MyInter1{ void m2(); } ============================================ interface MyInter1{ void m1(); } class MyClass extends MyINter1{ ==> Error } ===================================== interface MyInter1{ void m1(); } interface MyInter2 implements MyInter1{==> Error } =================================================== How to create a public interface: ================================= 1) click on SRC of package: new ==> Interface // Interface1 as Public type package AIT.IO.Pack.Java; public interface MyInterface1 { void m1(); } // Interface2 as Public type package AIT.IO.Pack.Java; public interface MyInterface2 extends MyInterface1{ void m2(); } package AIT.IO.Pack.Java; public class MainClass implements MyInterface2{ @Override public void m1() { System.out.println("m1() from Interface1"); } @Override public void m2() { System.out.println("m2() from Inteface2"); } public static void main(String[] args) { // TODO Auto-generated method stub } } ============================================ How to implements more than one interface to class: ==================================================== Multiple Inheritance: ===================== package AIT.IO.Pack.Java; interface CrudRepo{ void save(); void search(); } interface SortingRepo{ void sort(); } class OrderDao implements CrudRepo,SortingRepo{ @Override public void sort() { System.out.println("This is the sort() from SortingRepo Interface."); } @Override public void save() { System.out.println("This is save() from CrudRepo Intrefcae."); } @Override public void search() { System.out.println("This is search() from CrudRepo Intrefcae."); } } public class Main { public static void main(String[] args) { OrderDao od = new OrderDao(); od.sort(); od.save(); od.search(); } } ================================================== Three types of interfaces: 1) Normal Interface 2) Marker Interface 3) Functional Interface ==> when an interface can be defined with more than one abstract methods that interface is called as "Normal Interface". ==> When an interface can be empty that interface is called as Marker Interface". ==> When an interface define with only one abstract method that interface is called as "Functional Interface".