Interface
Interface
==========
Introduction:
style-1
ex-1:
interface Bank
{ int limit = 40000;
void roi();
}
class TestClient1
{ public static void main(String[] args)
{ AxisBank ax = new AxisBank();
ax.roi();
class TestClient2
{ void info1(AxisBank b)
{ b.roi();
}
void info2(SbiBank b)
{ b.roi();
}
public static void main(String[] args)
{ TestClient2 t = new TestClient2();
t.info1(new AxisBank());
t.info2(new SbiBank());
}
}
class TestClient3
{ void info(Bank b)
{ b.roi();
}
public static void main(String[] args)
{ TestClient3 t = new TestClient3();
t.info(new SbiBank());
t.info(new AxisBank());
}
}
Note:
a. One interface contains multiple implementation classes. So class to class they
can change the implmentation.
style-2
ex-2:
interface Operations
{ void add(int num1,int num2);
void mul(int num1,int num2);
}
class Test
{ public static void main(String[] args)
{ Dev2 op = new Dev2();
op.add(10,20);
op.mul(4,5);
}
}
interface Bank
{ void deposit();
void withdraw();
void loan();
void account();
}
class TestClient
{ public static void main(String[] rgs)
{ Dev3 d = new Dev3();
d.account();
d.loan();
d.deposit();
d.withdraw();
}
}
interfaces inheritace concept:
class extends class
interface extends interface
class implements interface
interface It1
{ void m1();
}
interface It2
{ void m2();
}
interface It3 extends It1,It2
{ void m3();
}
class Test implements It3
{ override 3 methods
}
case 5: If one class implements multiple interfaces, if the multiple interfaces are
having comman method
then override that method once in implementation class.
interface It1
{ void m1();
void m2();
}
interface It2
{ void m2();
void m3();
}
class Test implements It1,It2
{ override 3 methods
}
Note:When we have the extends & implements in the single,the extends keyword should
be first statement.
Nested interfaces:
case 1: Declaring interface inside the another interface.
interface It1
{ void m1();
interface It2
{ void m2();
}
}
class Test implements It1,It1.It2
{ overide 2-methods
}
Adaptor class:
This class constains Empty implementations of interface methods.
Limitation of interface:
If the interface contains 10-methods in implementation class must override 10
methods if we required
or not.
To overcome above problem to override required methods use adaptor class
concept.The adaptor class
is a normal java class contains empty implementation of interface methods.
Adaptor class coding convension : The class name must suffix with Adaptor
example:
interface It1
{ void m1();
void m2();
;;;;;
void m6();
}
ex: Realtime Development process : when we write the code remember below points.
class MyClass implements Myinterface
{ Must override all interface methods.
}
class MyClass extends Abstarctclass
{ Just override the abstarct methods present in abstarct class.
}
class MyClass extends Normalclass
{ Override required methods.
}
class MyClass extends Adaptorclass
{ Override required methods.
}
ex-1:
In Java SE 7 or earlier versions, an interface can have only two things i.e.
Constant variables(public static final variables)
Abstract methods.
interface Operations
{ String name = "ratan";
void add(int num1,int num2);
}
class Test implements Operations
{ public void add(int num1,int num2)
{ System.out.println("Your name..."+name);
System.out.println(num1+num2);
}
public static void main(String[] args)
{ Test t = new Test();
t.add(10,20);
}
}
ex-2:
From java-8 the interface allows three methods,
Constant variables
abstract method
default methods
static methods
interface Party
{ void eat();
default void comman_eat()
{ System.out.println("Ice creams, fruit salads.....");
}
static void wish()
{ System.out.println("Hi welcome to the party");
}
}
class TestClient
{ public static void main(String[] args)
{ Veg v = new Veg();
Party.wish();
v.eat();
v.comman_eat();
Note: inside the interface once we declare default & static methods with
implementation, so that all
implementation classes can use that implementions.
ex-3:
Java 9 onwards, inside the interface we can include,
Constant variables
abstract methods
default methods
static methods
private methods
private static methods
interface Operations
{ default void m1()
{ m3(); :: valid
m4(); :: valid
}
static void m2()
{ m3(); :: Invalid
m4(); :: valid
}
Note:
Inside the interface once we declare the private,private static methods these
implemention can be
used only with in the interface, it is not be accessed outside of the
interface.
Inside the interface once we declare the private,private static methods these
implemention can be
accessed with in the default & static methods of interface.
Inside the default method we can access the private & private static methods.
Inside the static methods we can access the only private static methods.
These private methods will improve code re-usability inside interfaces and
will provide choice to
expose only our intended methods implementations to users.
interface Temp {
public abstract void mul(int a, int b);
interface Message
{ public static void main(String[] args)
{ System.out.println("interface main method");
}
}
D:\>javac Test.java
D:\>java Message
interface main method
ex-5:
case 1: static methods can be accessed using interface name.
interface It1
{ static void m1()
{ System.out.println("It1 m1() method");
}
}
interface It2
{ static void m1()
{ System.out.println("It2 m1() method");
}
}
case 1:
interface It1
{ void m1();
}
case 2:
interface It1
{ void m1();
void m2();
}
case 3:
interface It1
{ void m1();
static void m2(){}
}
case 4:
interface It1
{ void m1();
static void m2(){}
default void m3(){}
}
case 5:
interface It1
{ void m1();
static void m2(){}
static void m3(){}
default void m4(){}
default void m5(){}
}
case 6:
interface It1
{ void m1();
static void m2(){}
default void m3(){}
private void m4(){}
private static void m5(){}
}
Q9. what is the structure we need to fallow while writing the code?
Ans: level-1 declare the interfaces
level-2 Abstract classes
level-3 normal classes/ implementation classes
level-4 client code to access the data.