Interfaces
Interfaces
Interfaces
Example1: Sun people responsible to define JDBC API and database vendor will
provide implementation for that.
Diagram:
Example2: Sun people define Servlet API to develop web applications web server
vendor is responsible to provide implementation.
Diagram:
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.
Syntax:
interface <interface_name>
// by default.
public abstract void wish();//in interface every method is public & abstract
w.wish();
w.msg();
}
Note:-The Java compiler adds public and abstract keywords before the interface
method. Moreover, it adds public, static and final keywords before data members.
In other words, Interface fields are public, static and final by default, and the
methods are public and abstract.
Ex:-
interface Printable {
void wish();
System.out.println("Hello");
Output:
Hello
interface Bank{
float rateOfInterest();
return 9.15f;
return 9.7f;
}
class TestInterface2{
System.out.println("ROI: "+b.rateOfInterest());
}}
Output:
ROI: 9.15
File: TestInterface1.java
interface Drawable{
void draw();
System.out.println("drawing rectangle");
}
class Circle implements Drawable{
System.out.println("drawing circle");
class TestInterface1{
d.draw();
}}
Note1:
Note2:
interface Interf{
void m1();
void m2();
Output:
D:\Java>javac SubServiceProvider.java
SubServiceProvider.java:1:
Extends vs implements:
Example:
class One{
Example:
interface One{
interface Two{
A class can extend a class and can implement any no. Of interfaces
simultaneously.
interface One{
void m1();
class Two{
}
}
Example:
interface One{
void m1();
interface Two{
void m2();
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 m1();
public void m1();
abstract void m1();
public abstract void m1();
As every interface method is always public and abstract we can't use the following
modifiers for interface methods.
Interface variables:
Example:
interface interf{
int x=10;
}
final: Implementation class can access this value but cannot modify.
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:
interface Interf{
int x;
Output:
D:\Java>javac Interf.java
1. int x;
Ans: 5
Example:
interface Interf{
int x=10;
}
Example 2:
int x=20;
System.out.println(x);
Output:
D:\Java>javac Test.java
D:\Java>java Test
20
interface Printable{
void print();
interface Showable{
void show();
System.out.println("Hello");
}
public void show(){
System.out.println("Welcome");
obj.print();
obj.show();
Output:Hello
Welcome
interface Printable{
void print();
}
interface Showable{
void print();
System.out.println("Hello");
obj.print();
Output:
Hello
As you can see in the above example, Printable and Showable interface have same
methods but its implementation is provided by class TestTnterface1, so there is no
ambiguity.
Interface inheritance
interface Printable{
void print();
void show();
System.out.println("Hello");
System.out.println("Welcome");
obj.print();
obj.show();
}
Output:
Hello
Welcome
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{
Example 2:
interface Right{
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:
interface Left{
Example 2:
interface Right{
}
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:
interface Left{
}
Example 2:
interface Right{
We can't write any java class that implements both interfaces simultaneously.
Yes, except if two interfaces contains a method with same signature but different
return types.
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;
Example 2:
interface Right{
int x=999;
}
Example 3:
//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
Example:
Serializable
Cloneable
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:
Example 3:
Without having any methods in marker interface how objects will get ability?
What is the difference between interface, abstract class and concrete class?
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Example of abstract class and interface in Java
interface A{
void b();
void c();
void d();
class M extends B{
class Test5{
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Output:
I am a
I am b
I am c
I am d
We can't create object for abstract class but abstract class can contain
constructor what is the need?
Abstract class constructor will be executed whenever we are creating child class
object to perform initialization of child object.
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
performance problems also.
Why abstract class can contain constructor whereas interface doesn't contain
constructor?
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.
Any Way we can’t Create Object for Abstract Class and Interface. But
Abstract Class can contain Constructor but Interface doesn’t why?
The Main Purpose of Constructor is to Perform Initialization of an Object
i.e. to
Provide Values for Instance Variables.
Abstract Class can contain Instance Variables which are required for Child
Class
Object to Perform Initialization of these Instance Variables Constructor is
required Inside Abstract Class.
But Every Variable Present inside Interface is Always public, static and final
whether we are declaring OR Not and Every Interface Variable we should
Perform
Initialization at the Time of Declaration and Hence inside Interface there is
No Chance of existing Instance Variable.
Due to this Initialization of Instance Variables Concept Not Applicable for
Interfaces.
Hence Constructor Concept Not required for Interface.