Interfaces
Interfaces
• There can be only abstract methods and final fields in the Java
interface, not method body.
• In other words, you can say that interfaces can have abstract methods
and variables.
• Java Interface also represents the IS-A relationship.
interface interfacename Interface is the keyword & interfacename is any java variable
{
variables declaration; variables can be declared as
methods declaration;
} static final type variablename = value; (variables are declared as constants)
Methods declaration will contain only a list of methods without any body
statements.
interface item
{
static final int code = 1001;
static final string name = “fan”;
void display();
}
DIFFERENCE BETWEEN CLASS AND INTERFACE
CLASS INTERFACES
The members of a class can be constant or variables The members of an interface are always declared as
constant, i.e their values are final.
The class definition can contain the code for each of its The methods in an interface are abstract in nature. i.e
methods. i.e the methods can be abstract or non- there is no code associated with them. It is later defined
abstract. by the class that implements the interface.
It can be instantiated by declaring objects It cannot be used to declare objects.It can only be
inherited by a class.
It can use various access specifiers like public, private It can only use the public access specifier.
or protected.
Difference between abstract class and interface
Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it
can have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static Interface has only static and final variables.
variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract
class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement An interface can extend another Java interface only.
multiple Java interfaces.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword
"implements".
8) A Java abstract class can have class members like private, protected, Members of a Java interface are public by default.
etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
How to declare an interface?
interface Area
{ class Interfacetest
final static float pi = 3.14f; {
float compute (float x, float y); public static void main(String args[ ])
} {
class Rectangle implements Area Rectangle rect = new Rectangle();
{ Circle cir = new Circle();
public float compute (float x , float y) Area area; //interface object
{ area = rect; //area refers to rect object
return (x* y); System.out.println(“Area of Rectangle =” +area.compute(10,20);
} area = cir; // area refers to cir object
} System.out.println(“Area of Rectangle =” +area.compute(10,0);
class circle implements Area
{
public float compute (float x , float y)
{
return (pi*x*x);
}
}
Interface Examples
interface Drawable{
Example 1: void draw();
}
interface printable{
class Rectangle implements Drawable{
void print(); public void draw()
} {
interface MyInterface {
void existingMethod();// abstract method with no body
}
Default Method in Interface :
If a class implements an interface that has a default method, the class can choose to:
- Use the default version of the method, or
- Provide its own version (i.e., override it).
class test{
interface Find{
public static void main(String args[]){
void show(); //abstract method
Find f=new test();
static int cube(int x)
f.show();
{
System.out.println(Find.cube(3));
return x*x*x;
}
}
}
}
class test implements Find{ Output :
public void show() computing cube
{
System.out.println(”computing cube"); 27
} Note : Find is the Interface name
}
Static Method in Interface: Why
ii)In cases in which a class implements two interfaces that both have same default
method,but the class does not override that method,then an error will result.
iii) In cases in which one interface inherits another,with both defining a common default
method,the inheriting interface’s version of the method takes precedence.