You are on page 1of 9

Interfaces

==========

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

interfaces are by default : abstract


interface methods are by default : public abstract
interface variable are by default : public static final

interface Bank // abstarct


{ int limit = 40000; // public static final
void roi(); // public abstract
}

class AxisBank implements Bank


{ public void roi()
{ System.out.println("Axis Bank ROI 9.99.... withdraw Limit"+Bank.limit);
}
}

class SbiBank implements Bank


{ public void roi()
{ System.out.println("SBI Bank ROI 11.99.... withdraw Limit"+Bank.limit);
}
}

class TestClient
{ public static void main(String[] args)
{ AxisBank ax = new AxisBank();
ax.roi();

SbiBank sb = new SbiBank();


sb.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();
}

abstract class Ratan implements Services


{ public void laptop()
{ System.out.println("This is Laptop service Center...");
}
}

class Ram extends Ratan


{ public void mobile()
{ System.out.println("This is mobile service Center...");
}
}

class TestClient
{ public static void main(String[] args)
{ Ram r = new Ram();
r.laptop();
r.mobile();
}
}

ex-3: Development process.


level 1 - interfaces : contains the declarations
level 2 - abstract classes : contians partial
implementations
level 3 - implementation class : contians all
implementations
level 4 - client code : Access the data.

interface Bank
{ void deposit();
void withdraw();
void loan();
void account();
}

abstract class Dev1 implements Bank


{ void deposit() {implementation here}
}
abstract class Dev2 extends Dev1
{ void withdraw(){implementation here}
}

class Dev3 extends Dev2


{ void account(){implementation here}
void loan(){implementation here}
}

class TestClient
{ public static void main(String[] rgs)
{ Dev3 d = new Dev3();
d.account();
d.loan();
d.deposit();
d.withdraw();
}
}

//interfaces vs. inheritace:


class extends class
interface extends interface
class implements interface

case 1 : one interface can extends another interface.


interface It1
{ void m1();
}
interface It2 extends It1
{ void m2();
}
interface It3 extends It2
{ void m3();
}
class Test implements It3
{ override 3 methods
}

case 2: one class can extends only one class at a time.


but one interface can extends multiple interfaces.

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 4: one class can implements multiple interfaces.


interface It1
{ void m1();
}
interface It2
{ void m2();
}
interface It3
{ void m3();
}

class Test1 implements It1


{ override 1 method.
}
class Test2 implements It1,It2
{ override 2 methods.
}
class Test3 implements It1,It2,It3
{ override 3 methods.
}

case 5:
interface It1
{ void m1();
void m2();
}
interface It2
{ void m2();
void m3();
}
class Test implements It1,It2
{ override 3 methods
}

possibilities: extends vs. implements


class A extends B ----> valid
class A extends B,C ----> invalid
class A extends A ----> invalid
class A implements It1 ----> valid
class A implements It1,It2 ----> valid

interface It1 extends It2 ----> valid


interface It1 extends It2,It3 ----> valid
interface It1 extends It1 ----> invalid
interface It1 implements A ----> invalid
interface It1 implements It2 ----> invalid

[extends must be first keyword]


class A extends B implements It1,It2 ---> valid
class A implements It1 extends B ---> invalid

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
}

case 2: declaring interface inside the abstract class.


abstract class A
{ abstract void m1();
interface It1
{ void m2();
}
}
class Test extends A implements A.It1
{ override 2-methods
}

case 3: declaring interface inside the normal class.


class A
{ interface It1
{ void m1();
}
}
class Test implements A.It1
{ override 1-method
}

Adaptor class: Empty implementations of interface methods


interface It1
{ void m1();
void m2();
;;;;;
void m6();
}
class X implements It1
{ void m1(){}
void m2(){}
;;;;;
void m6(){}
}

class Boys implements It1


{ must override 6-methods
}
class Girls extends X
{ override required 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.
}

Marker interface & Cloneing process:


class Test implements Cloneable
{ int a=10,b=20;
public static void main(String[] args)throws Exception
{
Test t = new Test();
System.out.println(t.a+" "+t.b);

Test copy = (Test)t.clone();


System.out.println(copy.a+" "+copy.b);
}
}

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.

c. The process of creating exactly duplicate object is called cloneing process.


d. To make the cloning process the class must implements Cloneable interface.
To create the cloning use clone() method & this method present in Object class.
protected native java.lang.Object clone() throws
java.lang.CloneNotSupportedException;
The clone() method throws CloneNotSupportedException, it is a checked
exception so handle the exception using try-catch or throws keyword.

java.lang.Cloneable ----> provides cloning capabilities


java.io.Serializable ----> provides Serialization capabilities
java.util.RandomAccess ----> data accessing capabilities

Note : user defined empty interfaces are not a marker interface.


interface It1
{
}

Day-3
Day-3 Interface java-8 features
=================================

ex-1: From java8 the interface allows three methods,


a. default methods
b. static method
c. abstract method

interface It1
{ void m1();
default void m2()
{ System.out.println("default method impl...");
}
static void m3()
{ System.out.println("static method impl.....");
}
}

class Test implements It1


{ public void m1()
{ System.out.println("m1 method impl");
}
public static void main(String[] args)
{ Test t = new Test();
t.m1();
t.m2();
It1.m3();
}
}

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 Veg implements Party


{ public void eat()
{ System.out.println("paneer dal sambar....");
}
}
class NonVeg implements Party
{ public void eat()
{ System.out.println("chiken mutton fish....");
}
}

class TestClient
{ public static void main(String[] args)
{ Veg v = new Veg();
v.eat();
v.comman_eat();

NonVeg nv = new NonVeg();


nv.eat();
nv.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");
}
}

class Test implements It1,It2


{ public void m1()
{ System.out.println("comman method impl....");
}
public static void main(String[] args)
{ Test t = new Test();
t.m1();
}
}

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.

ex-5: Functional interfaces


The interface contians only one abstract is called functional
interfaces.
The functional interface can have multiple default & static
methods.
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(){}
}

ex: functional interface with lambda expression


interface Message
{ void wish();
}

class Ratan implements Message


{ public void wish()
{ System.out.println("Good morning....");
}
}

class TestClient
{ public static void main(String[] args)
{ Ratan r = new Ratan();
r.wish();

Message m = ()-> System.out.println("Good morning....");


m.wish();
}

You might also like