Core Java Tutorial

 
 

Interface

Interfaces in java are similar to classes in java.Interfaces are used to achieve 1)Fully Abstraction(100%) 2)To support Multiple Inheritance(as we know that in inheritance concept multiple inheritance is not possible).Mainly java interfaces are used to provide contract to subclass to implement.

Key Points

  1. Interfaces are declared by using "interface" keyword.
  2. Interface don't allow Constructor declaration
  3. Interfaces in java will have abstract methods, default methods , static methods but method body will exist only for default methods and static methods.
  4. By default all the methods in interfaces are Abstract(with no Body) and Public.
  5. All the variables in interfaces are public,static and final.
  6. Interfaces can't be Instaniate.
  7. Class can extend only one Class
  8. Interface can extend one or more Interface
  9. Class can implement one or more Interface
     Ex:
        // interface java file     InterfaceDemo.java
        public  interface InterfaceDemo {
	String name = "venkat";
	void info();
	void info1();
	}

        // implementing interface using class
       public class InterfaceTest  implements InterfaceDemo{
	
	public void info(){
		System.out.println("implementing info method from interface");
		
	}
	public void info1(){
		System.out.println("implementing info1 method from interface");
	}
	
	public static void main(String[] args) {
		InterfaceTest interfaceTest = new InterfaceTest();
		interfaceTest.info();
		interfaceTest.info1();
	}
        }

Output: 
implementing info method from interface
implementing info1 method from interface