wellnwill Marathahalli Bangalore

interface


  • Interface is also one of the type definition block it can contain only public abstract methods and static final variables.
  • We cannot create an object of interface.
  • We can achieve 100% abstraction through interface because it contains only public abstract methods.
  • Interface doesn’t have constructor and initialization block.
  • Java compiler generates a separate byte code i.e. class file for each interface same as normal class.
  • To provide the implementation of interface abstract methods, we need to create a sub class which implements particular interface and this sub class is responsible to provide public definition of all the abstract methods.
  • If subclass is not able to provide the implementation of all the methods of implemented interface then make the subclass as abstract.
  • We can achieve runtime polymorphism through interface i.e. the reference variable of an interface can be used to hold the object of child class.
  • A class can implements an interface as well as can extends other class simultaneously.
  • An interface cannot inherit a class.
  • A class can implement multiple interface at a time.
  • An interface can extends multiple interface at a time.
  • Multiple inheritance is possible in java through interface.
interface Test1
{
  int i=10;
  void test1();
}

class Complete implements Test1
{
  public void test1()
  {
   System.out.println("test1() body");
  }
}

class Run
{
 public static void main(String args[])
 {
  Complete c1=new Complete();
  c1.test1();
 }
}

interface Test1
{
  int i=10;
  void test1();
}

class Complete implements Test1
{
  public void test1()
  {
   System.out.println("test1() body");
  }
}

class Run
{
 public static void main(String args[])
 {
  Test1 t1=new Complete();
  t1.test1();
 }
}


interface Test1{
  int i=10;
  void test1();
}
interface Test2{
  int j=20;
  void test2();
}
class Complete implements Test1,Test2
{
  public void test1()
  {
   System.out.println("test1() body");
  }
  
  public void test2()
  {
   System.out.println("test2() body");
  }
}

class Run
{
 public static void main(String args[])
 {
  Test1 t1=new Complete();
  t1.test1();
  Test2 t2=new Complete();
  t2.test2();
  System.out.println(Test1.i);
  System.out.println(Test2.j);
 }
}

interface Test1
{
  int i=10;
  void test1();
}

interface Test2
{
 void test2();
}

interface Test3 extends Test1,Test2
{
}

class Test implements Test3
{
 public void test1()
 {
  System.out.println("Test1()");
 }
public void test2()
 {
  System.out.println("Test2()");
 }
}
class InterfaceTest
{
 public static void main(String args[])
 {
  //Test1 t1=new Test1();
  System.out.println(Test1.i);
  //Test1.i=50; final variable
  Test1 t1=new Test();
  t1.test1();
  Test3 t2=new Test();
  t2.test2();
 }
}
Output-    10
           Test1()
           Test2()


interface I1
{
}
class A
{
}
class B extends A implements I1
{
public static void main(String args[])
{
System.out.println("Hi");
}
}
Output-CTS

interface I1
{
}
class A
{
}
class B implements I1 extends A 
{
public static void main(String args[])
{
System.out.println("Hi");
}
}
Output-CTE
	   

interface Test
{
}
class A extends Test
{
}
Output-CTE

interface Test
{
}
class B implements Test
{
}
Output-CTS
	   

interface Test1
{
}
interface Test2
{
}
class C implements Test1,Test2
{
}
Output-CTS


interface Test
{
}
class A
{
}
class D extends A implements Test
{
	
}
Output-CTS	
  

interface Test1
{
	
}
interface E implements Test1
{
	
}
Output-CTE

interface Test1
{
}
interface Test2
{
}
interface F extends Test1,Test2
{
}
Output-CTS  

interface Test1
{
 void m1();
}
interface Test2
{
 void m1();
}
class C implements Test1,Test2
{

 public void m1()
 {
 System.out.println("Hi");
 }
 
 public static void main(String a[])
 {
  C c1=new C();
  c1.m1();
 }
}
Note: Multiple inheritance is possible 
in the case of interface.

class Test1
{
 void m1()
 {
  System.out.println("m1() in Test1");
 }
}
class Test2
{
 void m1()
 {
  System.out.println("m1() in Test1");
 }
}

class C extends Test1,Test2
{
public static void main(String a[])
 {
  C c1=new C();
  c1.m1();
 }
}
Note: Multiple inheritance is 
not possible in the case of classes. 

Complete method inside interface: default method and static method


interface A
{
	
 static String name="nokia";
 void m1();
 
 default void m2()
 {
 System.out.println("use default keyword with method");
 } 
 
 static void cname()
 {
 System.out.println("name:"+name);
 }   
}  

class Test implements A
{  
 public void m1()
 {
 System.out.println("m1() of A");
 }  
}  
class Run
{  
 public static void main(String args[]){  
 A a1=new Test();  
 a1.m2();  
 a1.m1();
 A.cname(); 
 }
}  

interface A
{
  int i=10;
}
class Test
{
 public static void main(String args[])
 {
  //Test1.i=50; final variable
 }
}
Output-CTE

interface A
{
  int i=10;
}

class Test
{
 public static void main(String args[])
 {
  A a1=new A();
 }
} 
Output-CTE

interface BookMyShow
{
 void getBankOffer();
}
class CITIBank implements BookMyShow
{
   public void getBankOffer()
   {
    System.out.println("buy 1 get 1 free/friday");	
   }
}
class HSBC implements BookMyShow
{
   public void getBankOffer()
   {
    System.out.println("buy 2 get 2 free/saturaday");	
   }
}
class Test
{
 public static void main(String args[])
 {
	 BookMyShow b1;
	 b1=new CITIBank();
	 b1.getBankOffer();
	 b1=new HSBC();
	 b1.getBankOffer();
 }
}
interface Swiggy
{
 void serviceCharge();/
}
class PizzaHut implements Swiggy
{
 public void serviceCharge()
 {
  System.out.println("service charges-50 rs/order");
 }
}
class Dominos implements Swiggy
{
 public void serviceCharge()
 {
  System.out.println("service charges-70 rs/order");
 }
}
class A44
{
 public static void main(String args[])
 {
  Swiggy s1;
  s1=new PizzaHut();
  s1.serviceCharge();
  s1=new Dominos();
  s1.serviceCharge();
 }
}



Footer with Map