Interface

Definition 1:
Any service requirement specification (SRS) is called an interface.

Example 2:
Sun people define Servlet API to develop web applications web server vendor is responsible to provide implementation.

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

Definition 3:
Inside interface every method is always abstract whether we are declaring or not hence interface is considered as 100% pure abstract class.

Summary Definition:

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:

Note 1:

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

Note 2:
Whenever we are implementing an interface method compulsory it should be declared as public otherwise we will get compile time error.

Output:
Compile time error.
D:Java>javac SubServiceProvider.java
SubServiceProvider.java:1:
SubServiceProvider is not abstract and does not override
abstract method methodTwo() in lnterface
class SubServiceProvider extends ServiceProvider

Extends vs Implements:

A class can extends only one class at a time.

A class can implements any no of interfaces at a time.

An interface can extend any no of interfaces at a time.

A class can extend any no of classes at a time.

Which of the following is true?
1. A class can extend any no of classes at a time.
2. An interface can extend only one interface at a time.
3. A class can implement only one interface at a time.
4. A class can extend a class and can implement an interface but not both simultaneously.
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:

Output:
D:Java>javac Three.java
Three.java:5: ‘{I expected class Three implements One extends Two{

Interface Methods:

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();
public abstract Void methodOne();
public: To make this method available for every implementation class. abstract: Implementation class is responsible to provide implementation.

As every interface method is always public and abstract we can’t use the following modifiers for interface methods. Private, protected, final, static, synchronized, native, strictfp.

Inside interface which method declarations are valid?
1. public void method One(){}
2. private void methodOne();
3. public final void methodOne();
4. public static void methodOne();
5. public abstract void methodOneQ;
Ans: 5

Method Variable

An interface can contain variables. The main purpose of interface variables is to define requirement level constants.
Every interface variable is always public static and final whether we are declaring or not.

Example1:

public: To make it available for every implementation class.
static: Without existing object also we have to access this variable.
final: Implementation class can access this value but cannot modify.
Hence inside interface the following declarations are equal.

As every interface variable by default public static final we can’t declare with the following modifiers.
Private
Protected
Transient
Volatile

For the interface variables compulsory we should perform initialization at the time of declaration only otherwise we will get compile time error.

Example:

Output:
Compile time error.
D:Java>javac lnterf.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: 1

Example: 2

Output:
D:Java>javac Test.java
D:Java>java Test
20

Method Naming Conflicts:

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:

Example 2:

Example 3:

Output:
D:Java>javac Left.java
D:Java>javac Right.java
D:Java>javac Test.java

Case 2:
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

Example: 2

Example: 3

Output:
D:Java>javac Left.java
D:Java>javac Right.java
D:Java>javac Test.java

Case 3:
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

Example: 2

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

Example: 2

Example: 3

Output:
D:Java>javac Left.java
D:Java>javac Right.java
D:Java>javac Test.java
D:Java>java Test

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:
Serializable
Cloneable
RandomAccess- These are marked for some ability.

SingleThreadModel

.

.

Example 1:
By implementing Serializable 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:
By implementing Cloneable interface our object is in a position to provide exactly duplicate cloned object.

Without having any methods in marker interface how objects will get ability?
Internally JVM is responsible to provide required ability.

Why JVM is providing the required ability in marker interfaces?
To Reduce complexity of the programming.

Is it possible to create our own marker Interface?
Yes, but customization of JVM must be required.
Ex : Sleepable , Jumpable

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

Example: 2

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

Example: 2

Example:

GenericServlet simply acts as an adapter class to Servlet Interface.
Note: marker interface and Adapter class are big utilities to the programmer to simplify programming.

Question Time:

What is the difference between interface, abstract class and concrete class? When we should go for interface, abstract class and concrete class?

If we don’t know anything about implementation just we have requirement specification then we should go for interface.
If we are talking about implementation but not completely (partial implementation) then we should go for abstract class.
If we are talking about implementation completely and ready to provide service then we should go for concrete class.

Difference between Interface & Abstract Class?


Previous Page


Next Page