Interfaces
Interfaces
Example2: Sun people define Servlet API to develop web applications web server
vendor is responsible to provide implementation.
Diagram
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Def2: From the client point of view an interface define the set of services what is
expecting. From the service provider point of view an interface defines the set of
services what is offering. Hence an interface is considered as a contract between client
and service provider.
Example: ATM GUI screen describes the set of services what bank people offering, at
the same time the same GUI screen the set of services what customer is expecting
hence
this GUI screen acts as a contract between bank and customer.
Def3: Inside interface every method is always abstract whether we are declaring or
not hence interface is considered as 100% pure abstract class.
Summery def: Any service requirement specification (SRS) or any contract between
client and service provider or 100% pure abstract classes is considered as an interface.
Declaration and implementation of an interface:
Note1:
Whenever we are implementing an interface compulsory for every method of that
interface we should provide implementation otherwise we have to declare class as
abstract in that case child class is responsible to provide implementation for remaining
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
methods.
Note2:
Whenever we are implementing an interface method compulsory it should be
declared
as public otherwise we will get compile time error.
Example:
interface Interf
{
void methodOne();
void methodTwo();
}
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Extends vs implements:
Extends vs implements:
A class can extends only one class at a time.
Example:
class One{
public void methodOne(){
}
}
class Two extends One{
}
Example:
interface One{
public void methodOne();
}
interface Two{
public void methodTwo();
}
class Three implements One,Two{
public void methodOne(){
}
public void methodTwo(){
}
}
A class can extend a class and can implement any no. Of interfaces simultaneously.
interface One{
void methodOne();
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
}
class Two
{
public void methodTwo(){
}
}
class Three extends Two implements One{
public void methodOne(){
}
}
An interface can extend any no. Of interfaces at a time.
Example:
interface One{
void methodOne();
}
interface Two{
void methodTwo();
}
interface Three extends One,Two
{
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
5. An interface can implement any no.Of interfaces at a time.
6. None of the above.
Ans: 6
Consider the expression X extends Y for which of the possibility of X and Y this
expression is true?
1. Both x and y should be classes.
2. Both x and y should be interfaces.
3. Both x and y can be classes or can be interfaces.
4. No restriction.
Ans: 3
X extends Y, Z ?
X, Y, Z should be interfaces.
X extends Y implements Z ?
X, Y should be classes.
Z should be interface.
X implements Y, Z ?
X should be class.
Y, Z should be interfaces.
X implements Y extend Z ?
Example:
interface One{
}
class Two {
}
class Three implements One extends Two{
}
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Output:
Compile time error.
D:\Java>javac Three.java
Three.java:5: '{' expected
class Three implements One extends Two{
Interface method:
Every method present inside interface is always public and abstract whether we are
declaring or not. Hence inside interface the following method declarations are equal.
void methodOne();
public Void methodOne();
abstract Void methodOne(); Equal
public abstract Void methodOne();
Example:
interface interf
{
int x=10;
}
int x=10;
public int x=10;
static int x=10;
final int x=10; Equal
public static int x=10;
public final int x=10;
static final int x=10;
public static final int x=10;
As every interface variable by default public static final we can't declare with the
following modifiers.
o Private
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
o Protected
o Transient
o Volatile
For the interface variables compulsory we should perform initialization at the
time of declaration only otherwise we will get compile time error.
Example:
interface Interf
{
int x;
}
Output:
Compile time error.
D:\Java>javac Interf.java
Interf.java:3: = expected
int x;
Which of the following declarations are valid inside interface ?
1. int x;
2. private int x=10;
3. public volatile int x=10;
4. public transient int x=10;
5. public static final int x=10;
Ans: 5
Interface variables can be access from implementation class but cannot be modified.
Example:
interface Interf
{
int x=10;
}
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Example 1:
Example
Example 2:
class Test implements Interf
{
public static void main(String args[]){
int x=20;
//here we declaring the variable x.
System.out.println(x);
}
}
Output:
D:\Java>javac Test.java
D:\Java>java Test
20\
Case 1:
If two interfaces contain a method with same signature and same return type in the
implementation class only one method implementation is enough.
Example 1:
interface Left
{
public void methodOne();
}
Example 2:
interface Right
{
public void methodOne();
}
Example 3:
class Test implements Left,Right
{
public void methodOne()
{
}}
Output:
D:\Java>javac Left.java
D:\Java>javac Right.java
D:\Java>javac Test.java
Case 2:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
if two interfaces contain a method with same name but different arguments in the
implementation class we have to provide implementation for both methods and these
methods acts as a overloaded methods
Example 1:
interface Left
{
public void methodOne();
}
Example 2:
interface Right
{
public void methodOne(int i);
}
Example 3:
class Test implements Left,Right
{
public void methodOne()
{
}
public void methodOne(int i)
{
}}
Output:
D:\Java>javac Left.java
D:\Java>javac Right.java
D:\Java>javac Test.java
Case 3:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
If two interfaces contain a method with same signature but different return types then
it
is not possible to implement both interfaces simultaneously.
Example 1:
interface Left
{
public void methodOne();
}
Example 2:
interface Right
{
public int methodOne(int i);
}
We can't write any java class that implements both interfaces simultaneously.
Is a java class can implement any no. Of interfaces simultaneously ?
Yes, except if two interfaces contains a method with same signature but different
return
types.
Variable naming conflicts:
Two interfaces can contain a variable with the same name and there may be a chance
variable naming conflicts but we can resolve variable naming conflicts by using
interface names.
Example 1:
interface Left
{
int x=888;
}
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Example 2:
interface Right
{
int x=999;
}
Example 3:
class Test implements Left,Right
{
public static void main(String args[]){
//System.out.println(x);
System.out.println(Left.x);
System.out.println(Right.x);
}
}
Output:
D:\Java>javac Left.java
D:\Java>javac Right.java
D:\Java>javac Test.java
D:\Java>java Test
888
999
Marker interface:
Marker interface:
If an interface doesn't contain any methods and by implementing that interface if our
objects will get some ability such type of interfaces are called Marker interface (or)
Tag
interface (or) Ability interface.
Example:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Serializable
Cloneable
RandomAccess These are marked for some ability
SingleThreadModel
.
.
.
.
Example 1:
By implementing Serilaizable interface we can send that object across the network
and
we can save state of an object into a file.
Example 2:
By implementing SingleThreadModel interface Servlet can process only one client
request at a time so that we can get "Thread Safety".
Example 3:
Without having any methods in marker interface how objects will get ability ?
Internally JVM is responsible to provide required ability.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Adapter class:
Adapter class:
Adapter class is a simple java class that implements an interface only with empty
implementation for every method.
If we implement an interface directly for each and every method compulsory we
should provide implementation whether it is required or not. This approach
increases length of the code and reduces readability.
Example 1:
interface X{
void m1();
void m2();
void m3();
void m4();
//.
//.
//.
//.
void m5();
}
Example 2:
class Test implements X{
public void m3(){
System.out.println("m3() method is called");
}
public void m1(){}
public void m2(){}
public void m4(){}
public void m5(){}
}
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
We can resolve this problem by using adapter class.
Instead of implementing an interface if we can extend adapter class we have to
provide implementation only for required methods but not for all methods of
that interface.
This approach decreases length of the code and improves readability.
Example 1:
abstract class AdapterX implements X{
public void m1(){}
public void m2(){}
public void m3(){}
public void m4(){}
//.
//.
//.
public void m1000(){}
}
Example 2:
public class Test extend AdapterX{{
public void m3(){
}}
Example:
Generic
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Generic Servlet simply acts as an adapter class for Servlet interface.
Note : marker interface and Adapter class are big utilities to the programmer to
simplify programming.
What is the difference between interface, abstract class and concrete class?
When we should go for interface, abstract class and concrete class?
What is the difference between interface,abstract class and concrete class?
When we should go for interface,abstract class and concrete class?
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Example:
Interface-
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
We can't declare interface methods with the
modifiers private, protected, final, static,
synchronized, native, strictfp.
Abstract class-
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
Every abstract class variable need not be
public static final.
We can't create object for abstract class but abstract class can contain
constructor what is the need ?
abstract class constructor will be executed when ever we are creating child class object
to perform initialization of child object.
Example:
class Parent{
Parent()
{
System.out.println(this.hashCode());
}
}
class child extends Parent{
child(){
System.out.println(this.hashCode());
}
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
}
class Test{
public static void main(String args[]){
child c=new child();
System.out.println(c.hashCode());
}
}
Note : We can't create object for abstract class either directly or indirectly.
Every method present inside interface is abstract but in abstract class also we
can take only abstract methods then what is the need of interface concept ?
We can replace interface concept with abstract class. But it is not a good
programming
practice. We are misusing the roll of abstract class. It may create performence
problems
also.
(this is just like recruiting IAS officer for sweeping purpose)
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
If every thing is abstract then it is recommended to go for interafce.
Why abstract class can contain constructor where as interface doesn't contain
constructor ?
The main purpose of constructor is to perform initialization of an object i.e., provide
values for the instance variables, Inside interface every variable is always static and
there is no chance of existing instance variables. Hence constructor is not required for
interface.
But abstract class can contains instance variable which are required for the child
object
to perform initialization for those instance variables constructor is required in the case
of abstract class.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, Email: [email protected]