Interface

What is interface?

  • Interface allows Abstraction, Only abstract methods are allowed
  • On implementation of an interface, you must override all of its methods
  • A class can implements multiple interfaces
  • Interface defines the body to the method
  • Like abstract classes, interfaces cannot be used to create objects
  • It is just a contract between classes.
  • Interfaces allow use to polymorphically represent class.

 1
                      
                        
                        interface Revenue{}  //Interface Revenue
                        class Marketing implements Revenue{} //Marketing class is
                        // a concrete class that implements Revenue interface
                        
                    
                

Why an interface?

  • To solve inheritance problem
  • We use implements keyword when we use interface
  • For e.g. Class A implements Interface
  • methods declared in interface are by default abstract (only method signature, no body).

 
                      
                        interface Animal {
                            void eat();
                        
                            void travel();
                        
                            void sleep();
                        }
                        
                        public class Mamal implements Animal {
                        
                            public void eat() {
                                System.out.println("Mammal eats");
                            }
                        
                            public void travel() {
                                System.out.println("Mammal travels");
                            }
                        
                            public int noOfLegs(){
                                return 0;
                            }
                            public void sleep(){
                                System.out.println("Mammal sleeps");
                            }
                        
                            public static void main(String[] args) {
                                Mamal m = new Mamal();
                                m.eat();
                                m.travel();
                                m.sleep();
                            }
                        }
                        
                    
                

Example of Interface code


					  
                        interface MotorBike {
                            void run();
                        
                        }
                        
                        public class InterfaceEg implements MotorBike {
                        
                            public static void main(String args[]) {
                                InterfaceEg m = new InterfaceEg();
                                m.run();
                            }
                        
                            public void run() {
                                System.out.println("running safely.. ");
                            }
                        }
					
					

Multiple Interfaces

Sample code on Github

                            

Interface e.g

Interface e.g

Interface e.g

Java Docs