Professional Documents
Culture Documents
==========
style-1
ex-1:
Interfaces are used to declare the functionalities of the project.
declare the interface using interface keyword
interfaces also .class files are generated
class TestClient
{ public static void main(String[] args)
{ AxisBank ax = new AxisBank();
ax.roi();
class TestClient
{ void info(Bank b)
{ b.roi();
}
public static void main(String[] args)
{
TestClient t = new TestClient();
t.info(new SbiBank());
t.info(new AxisBank());
}
}
Note:
a. One interface contains multiple implementation classes.
b. inside the interface not possible to declare instance variables.
c. inside the interfaces constructors not allowed.
d. inside the interface instance,static blocks are not possible.
e. Inside the interface main method is not allowed.
style-2
ex-2:
interface Services
{ void laptop();
void mobile();
}
class TestClient
{ public static void main(String[] args)
{ Ram r = new Ram();
r.laptop();
r.mobile();
}
}
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();
}
}
interface It1
{ void m1();
}
interface It2
{ void m2();
}
interface It3 extends It1,It2
{ void m3();
}
class Test implements It3
{ override 3 methods
}
case 3:
interface It1
{ void m1();
void m2();
}
interface It2
{ void m2();
void m3();
}
interface It3 extends It1,It2
{ void m4();
}
class Test implements It3
{ override 4 methods
}
case 5:
interface It1
{ void m1();
void m2();
}
interface It2
{ void m2();
void m3();
}
class Test implements It1,It2
{ override 3 methods
}
Day-2
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
}
ex: 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.
}
a. The marker interface does not contain any methods but whenever our class
implements that interface our class acquire some capabilities to perform some
operations, such type of interfaces are called marker interfaces.
b. Marker interface is empty interface but class is acquiring capabilities these
capabilities are provided by JVM.
Day-3
Day-3 Interface java-8 features
=================================
interface It1
{ void m1();
default void m2()
{ System.out.println("default method impl...");
}
static void m3()
{ System.out.println("static method impl.....");
}
}
ex-2:
1. To give comman implementation to all implementation classes, then declare the
comman implementions inside the interface using default & static methods.
2. default method implementations it is possile to override.
static method implementations are not possible to override.
interface Party
{ void eat();
default void comman_eat()
{ System.out.println("Ice creams, fruit salads.....");
}
}
class TestClient
{ public static void main(String[] args)
{ Veg v = new Veg();
v.eat();
v.comman_eat();
ex-3: Inside the interface possible to declare the main method from java8
interface It1
{
public static void main(String[] args)
{
System.out.println("interface main method");
}
}
ex-4:
interface It1
{ default void m1()
{ System.out.println("It1 m1() method");
}
}
interface It2
{ default void m1()
{ System.out.println("It2 m1() method");
}
}
When the class implements multiple interfaces, If the multiple iterfaces contains
same default methods then we will get incompatible error.
to overcome above problem override the default in the implementation class, then at
runtime override method will be executed.
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(){}
}
class TestClient
{ public static void main(String[] args)
{ Ratan r = new Ratan();
r.wish();