Interfaces:

It can contain only below things. 1.Constants. 2.Function declarations. 3.Nested types(classes or Interfaces). Syntax:
       interface interface_name
       {
    
        }
For constants:
  Interface interface_name
  {
      1. public static final int i=10;
   // int i=10;
      int j;  //error

  Function declarations:
       2.public void display();
}

Rules for declaring functions in interfaces:

--> Interface should have only public methods. --> If programmer does not mention any access modifier , it will automatically treated as public. --> Interfaces should not have constructor. The reason is you cannot create objects for interfaces. --> Interfaces should not have static functions. NOTE: After java1.7 they may include static functions in interfaces. Example: public void disp(); Rule 1 void test(); Rule 2 compile error // public static void fun(); Rule 4 Interfaces should not have function definitions // void test() { System.Out.Println("test"); Rule 5 } --> By default all the function in interfaces are Abstract methods. --> Any method that is not having function definition or function body is called "Abstract method" or "Abstract body". Example:
	interface MyInterface
	{
  	      int i=10;
   	      static int j=200;
   	      public static final int k;
   	      public MyInterface();
  	      protected void disp();
  	      public void test()
 	      {
  	       }
	 }
          interface MyInterface
	  {
	          public static final int i=10;
	          public static final int j=20;
	          public static final int k=30;
  	          public abstract void disp();
 	          public abstract void test();
  	          public class student
                  {
  	              int i=10;
  	              public void func()
 	              {
  	                   System.out.println(i);
  	               }
	          }
	   }

Creating interfaces:

          class Rect implements RectInterface
          {
                  int len,bred;
                  public Rect(int l, int b)
                  {
                     len = l;
                     bred = b;
                  }
                  public void findArea()
                  {
                      System.out.println(len*bred);
                   }
          }

            interface RectInterface
            {
                public void findArea();
            }
When a class implements an interface then it is telling to the compiler that it is implementing all the functions of that interface.If that class do not implement any of the function of that interface then compiler will generate an error. --> If the class do not want to implement all the functionality of that interface then it can declare itself as "abstract class".

Uses of interface:

1.It will be used as API s (Application Program Interface) when you are giving library code to your clients. Interface
2. It will be used as contract between two module developers of a project. 3.You can implement "multiple inheritance" using interfaces. 4. If class has only declaration then it is an interface.
       interface Shape
       { 
            public void draw();
            public void findArea();
       }
If you don't know the functionality of a class but if you know what function to do then you can convey that message to future project engineers by creating an interface.

Creating Interface for circle:

   	     class Circle implements CircleInterface 
             {
	             int rad;
 	             public Circle(int r)
	             {
    	                  rad=r;
  	              }
	       public void findArea()
	       {
 	             System.out.println(pi*rad*rad);
	        }
	     }	
                 interface CircleInterface
	         {
  	            final double pi=3.14;
  	            public void findArea();
	         }
	     public class MainClass
	     {
	            public static void main(String[] args)
	            {
    	                 Circle c = new Circle(3);
                 	 c.findArea();
 	             }
	       }

Is-A relationship with Interfaces:

	        class Vehicle
	        {
	             public void run()
	             {
 	                 System.out.println("I can run");
	              }
	         }

	        class Cycle extends Vehicle
	         {
    	            public void run()
  	            {
    	                  System.out.println("I can run on 2 wheels");
  	             }
		 }
	        public class MainClass 
	        {
	            public static void main(String[ ] args)
  	            {
    	                   Vehicle v=new Cycle();
    	                   v.run();
  	            }
	        }
--> When you say "implements" and you are not really implementing all the functions of interface then java compiler will catch the errors. Assignment: 1.Client-server implement with interface. Interface1
Q: Why you cannot create objects for interface? Ans: Reason is, since there are no function definition in interface even if you create object you cannot access any functionality.Hence there is no point in creating object. Can you extend interface?
            interface BaseInterf
	    {
   	        public void test();
	     }
	     interface DerInterf extends BaseInterf
	     {
    	          public void disp();
	      }
Note: You can extend interface but into the other interface but not into the class.
 
	   interface BaseInterf
	   {
 	         public void test();
	   }
	   class DerInterf implements BaseInterf
	   {
 	          public void disp();
	    }
	
You cannot implement 1 interface in another.Reason is that statement is wrong because when you implementing it will become a class but not interface. Can we implement multiple interfaces in a class:
	          interface HumanInterf
	         {
 	                 public void workWithHands();
	          }
	         interface RobotInterf
	         {
	                 public void workWithCpu();
	         }
	       class Emp implements HumanInterf, RobotInterf
	        {
  	                public void workWithHands()
 	                {
  	                   System.out.println("I'm human");
	                 }
	                 public void workWithCpu()
	                 {
   	                    System.out.println("I'm robot");
 	                 }
	     }
Assignment: Can I extend multiple interfaces into another interface:
 
	  interface HumanIntr
	  {
  	       public void workWithHands();
  	       public void doWork();
	  }
	 interface RobotIntr
	 {
	      public void workWithCPU();
  	      public void doWork();
	  }
	  interface EmpIntr extends HumanIntr,RobotIntr
	  {
 	       public int getSal();
	    }
Interface2
   	     interface EmpInterf
	     {
 	          public int getSal();
 	          public String getName();
	    }
           interface EmpIntrv2 extends EmpInterf
	   {
  	         public double getPf();
	    }
	    class Emp implements EmpInterf
	    {
 	             int eno,esal;
   	             String ename;
	             public Emp(int no,String name, int sal)
	            {
  	                eno=no;
                	ename=name;
    	                esal=sal;
	            }
	       public int getSal()
 	        {
 	           return esal;
 	         }
 	       public String getName()
	       {
	           return ename;
 	       }
	     }

Modifying interfaces:

--> When you are modifying given interface which is already released in the market then you are not supposed to change the existing interface. --> The reason is it may break the existing client's code. So we have to create new interface with new version number by extending the older interface.

Implementing multiple interfaces:

Interface3
            interface HumanInterface
	    {
	          public void workWithHands();
	    }
	    interface RobotInterface
	    {
	         public void workWithCPU();
	    }
	    class Human implements HumanInterface
	    {
	         public void workWithHands()
  	         {
   	              System.out.println("I'm human,I can work with hands");
   	          }
	    }
           class Robot implements RobotInterface
	   {
	         public void workWithCPU()
	         {
		         System.out.println("I m a robot, I can work with CPU");
	         }
            }
	   class Emp extends Human implements RobotInterface
	   {
   	          private int eno, speed;
  	          private String ename;
                  private Robot r=new Robot();
    	          private Human h=new Human();
	          public Emp(int no, String name)
    	          {
                	eno = no;
                	ename = name;
    	           }
   	         public Emp(int no, String name, int spd)
    	         {
     	             eno = no;
     	             ename = name;
                     speed = spd;
    	         }
   	          public void workWithCPU()
  	          {
          	       if(speed==0)
     	               {
         	           System.out.println("Sorry, I'm human being" + "I can't work with CPU");
      	                }
     	               else
     	               {
       	                 r.workWithCPU();
       	                }
      	            }    
  	          public void workWithHands()
    	          {
      	                  if(speed!=0)
     	                  { 
       	                     System.out.println ("Sorry, I'm robot, "+ "I cant work with hands");
      	                  }
            	         else
      	                 {
       	                     super.workWithHands ();
        	             h.workWithHands ();
       	                  }
                   }
                 }

	           public class EmpDemo
  	           {
    	                public static void main(String[ ] args)
   	                 {
   	                       Emp h = new Emp (1, "steeve");
     	                       Emp r= new Emp (2,"robo",1000);
       	                       h.workWithHands ();
        	               h.workWithCPU ();
       	                       r.workWithCPU ();
                               r.workWithHands ();
                        }
                    }