0% found this document useful (0 votes)
4 views

Interface

Uploaded by

nnnaik406
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Interface

Uploaded by

nnnaik406
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Interfaces

==========
Introduction:

Interface provides service details but not its implementation details.


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

style-1
ex-1:
interface Bank
{ int limit = 40000;
void roi();
}

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 TestClient1
{ public static void main(String[] args)
{ AxisBank ax = new AxisBank();
ax.roi();

SbiBank sb = new SbiBank();


sb.roi();

Bank b1 = new AxisBank();


b1.roi();

Bank b2 = new SbiBank();


b2.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.

b. Inside the interface not possible to declare instance variables. Because by


default interface variables are constants(public static final).

c. Inside the interfaces constructors not allowed.


Inside the interface instance,static blocks are not possible.
Inside the interface main method is not allowed.

d. Inside the interface static,final,private methods declarations are not possible.


Because interface methods are must override in implementation class
but private,static,final methods are not possible to override.

e. While overriding interface methods, the implementation methods must be public


method becuase the interface methods are by defauult public.

style-2
ex-2:
interface Operations
{ void add(int num1,int num2);
void mul(int num1,int num2);
}

abstract class Dev1 implements Operations


{ public void add(int num1,int num2)
{ System.out.println(num1+num2);
}
}

class Dev2 extends Dev1


{ public void mul(int num1,int num2)
{ System.out.println(num1*num2);
}
}

class Test
{ public static void main(String[] args)
{ Dev2 op = new Dev2();
op.add(10,20);
op.mul(4,5);
}
}

1. The interface contains multiple abstract methods, so write the implementation


in implementation
classes.If the implementation class is unable to provide implementation of all
abstract
methods, then declare the implementation class with abstract modifier, and
complete the
remaining method implementations in next created child classes.

2. It is possible to declare multiple child classes but at final we have complete


the
implementation of all abstract methods.

ex-3: In Realtime Development process.


level 1 - interfaces : contains the service details

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 inheritace concept:
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: If one interface extends 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();
}
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: 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
}

Possibilities of 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

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


class A implements It1 extends B ---> Invalid

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
}

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:
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.

Note : If our class implementing interface must override all methods.


But If our class extending adaptor class it is possible to override
required methods.

Adaptor class coding convension : The class name must suffix with Adaptor

example:
interface It1
{ void m1();
void m2();
;;;;;
void m6();
}

class XAdaptor implements It1


{ void m1(){}
void m2(){}
;;;;;
void m6(){}
}

class Dev1 implements It1


{ must override 6-methods
}
class Dev2 extends XAdaptor
{ override required methods
}

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 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();
Party.wish();
v.eat();
v.comman_eat();

NonVeg nv = new NonVeg();


Party.wish();
nv.eat();
nv.comman_eat();
}
}
1. To give comman implementation to all implementation classes, then declare the
comman
implementions inside the interface using default & static methods. So that the

implementataions can be used by all implementation classes.

2. default method implementations it is possile to override in any implementation


classes.
static method implementations are not possible to override.

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
}

private void m3()


{ logics here
}
private static void m4()
{ logics here
}
}

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);

default void add(int a, int b)


{ sub(a, b); // private method inside default method
div(a, b); // static method inside other non-static method
System.out.print("Answer by Default method = ");
System.out.println(a + b);
}
public static void mod(int a, int b)
{ div(a, b); // static method inside other static method
System.out.print("Answer by Static method = ");
System.out.println(a % b);
}

private void sub(int a, int b)


{ System.out.print("Answer by Private method = ");
System.out.println(a - b);
}
private static void div(int a, int b)
{ System.out.print("Answer by Private static method = ");
System.out.println(a / b);
}
}

class TestImpl implements Temp{


public void mul(int a, int b)
{ System.out.print("Answer by Abstract method = ");
System.out.println(a * b);
}
public static void main(String[] args)
{ Temp impl = new TestImpl();
impl.mul(2, 3);
impl.add(6, 2);
Temp.mod(5, 3);
}
}
ex-4: Inside the interface possible to declare the main method from java-8 version.

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");
}
}

class Test implements It1,It2


{ public static void main(String[] args)
{ It1.m1();
It2.m1();
}
}

case 2: instance methods(default methods)


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("implementation class override m1() method");
}
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
method in the
implementation class, then at runtime override method will be executed.
ex-6: java-8 : Functional interfaces
The interface contians only one abstract method is called functional
interfaces.
The functional interface can have multiple default & static & private &
private static methods.
The lambda expression will provide the implementation of Functional
Interface.

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(){}
}

Q1. what are the advantages of interfaces?


Ans: Interfaces are used to declare the functionalities of the project.
Interface provides service details but not its implementation details.

Q2. can we create the object for iterface?


Ans: No, because iterfaces are by default abstract.
Q3. is it interfaces supports inheritance?
Ans: yes, possible

Q4. What is the difference between abstract classes & interface?


Ans: Interface provides service details but not its implementation details.
Abstract classes are comes from interfaces which contians declarations &
implementation.
(abstract class contians partial implementataions)

Q5. Is it one interface can extends multiple interfaces?


Ans: yes, possible

Q6. inside the interaface can we declare final methods?


Ans: no not possible, because interface methods must override in implementation
classes, where as final methods not possible to override.

Q7. is it one class can implements Multiple interfaces?


Ans: yes, possible

Q8. Inside the interface can we declare instance varaibles?


Ans: no, not possible because interface variables are by default "public static
final".

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.

Q10. For the interfaces compiler generated .class files or not?


Ans: yes compiler generates .class files.

Q11. What is adaptor class?


Ans: It contains empty implementation of interface methods.

Q12. what is marker interface?


Ans: The interface without methods but providing some capabilities to
implementation classes.
Cloneable : gives cloneing capabilities.
Serializable : gives serialization capabilities.
The interface with or without methods but providing some capabilities.

Q13. Define nested interface?


Ans: Declaring interface inside another interface.
Declaring interface inside abstract class.
Declaring interface inside normal class.

Q14. what is the cloneing process?


Ans: The process of creation duplicate object is called cloneing.

Q15. what are the types of cloneing?


Ans: Deep cloening : creation of two duplicate objects
Shallow cloning : creating duplicate references to same object.

Q16. what is functional interface?


Ans: The interface contains only one abstract method, but may allows multiple
default & static methods.

Q17. what are the java9 features w.r.t interface?


Ans: it allows private,private static methods.

Q18. Why the interface allows default & static methods?


Ans: from java8 version onwards interface allows default , static methods.
To provide the commans implementations to all implementation classes.
Declare the default,static
methods in interface.

Q19. Inside the interface can we define main method?


Ans: upto java-7 version not possible.
from java-8 onwards interface allows main method.

Q20. Is it possible to prevent inheritance w.r.t interfaces?


Ans: no, not possible If we are using fianl modifier we will get error.

You might also like