II BCA - CORE PAPER 3
PROGRAMMING IN JAVA
USE INTERFACES
Contents:
- Java Interface
- Need for Java interface
- Declaring an Interface
- Relationship between Interface & class
- Declaring Interface Constants
Java Interface
- · The interface in Java is a mechanism to achieve abstraction that specify what a class must do without saying anything about how the class will do it.
- · An interface can be defined as a container that stores the methods to be implemented in the code segment.
- · An interface can have methods and variables just like the class but the methods declared in interface are by default abstract.
- · The variables declared in an interface are public, static & final by default.
Need for Java interface
There are
mainly three reasons to use interface.
1.
It is used to achieve abstraction.
2.
By interface, we can support the
functionality of multiple inheritances.
3.
It can be used to achieve loose coupling.
Declaring an interface
·
An interface is declared by
using the interface keyword.
·
It provides total abstraction,
where all the methods in an interface are declared with the empty body, and all
the fields are public, static and final by default.
·
A class that implements an
interface must implement all the methods declared in the interface.
Syntax:
interface <interface_name> {
// declare constant fields
// declare methods that abstract by default.
}
Example:
interface MyInterface
{
public void
method1();
public void
method2();
}
The
relationship between classes and interfaces
·
A class extends another class,
an interface extends another interface, but a class
implements an interface.
·
Though a class and an Interface
look a little similar, they are extremely different from each other.
·
Some of the primary differences
between class & Interface are given below as follows.
Interface |
Class |
Keyword used: interface |
Keyword used: class |
Interfaces do not have a constructor |
Class includes a constructor |
Interface stores only the signature of a method |
Class stores complete method definition |
Interfaces do not need Access Specifiers |
In Class, Access Specifiers are mandatory |
Interfaces do not include Data Members |
Class includes Data Members |
Interfaces do not have Static Members |
Class includes Static Members |
- ·
All interface methods are implicitly public and abstract. In other
words, you do not need to actually type the public or abstract modifiers in the method declaration, but the
method is still always public and abstract.
- ·
All variables defined in an interface must be public, static, and final—in other
words, interfaces can declare only constants, not instance variables.
- ·
Interface methods must not be static.
- ·
An interface can extend one or more
other interfaces.
- ·
An interface cannot extend anything but
another interface.
- · An interface cannot implement another interface or class.
- ·
An interface must be declared with the keyword interface.
- ·
Interface types can be used polymorphically.
Example:
interface Drawable{
void draw();
}
class Rectangle implements
Drawable{
public void draw(){
System.out.println("Drawing
rectangle");
}
}
class Circle implements
Drawable{
public void
draw(){
System.out.println("Drawing
circle");
}
}
public class TestInterface1{
public static
void main(String args[]){
Drawable d=new Circle();//In real scenario, object is
provided by method e.g. getDrawable()
d.draw();
}
}
Compile :
javac TestInterface1.java
Run :
java TestInterface1
Output : Drawing circle
Declaring Interface Constants
- ·
A Java interface can contain constants. By placing the constants right in the interface, any class that implements
the interface has direct access to the constants, just as if the class had
inherited them.
- · Generally interface constants are defined in an interface & they don't have to be declared as public, static, or final.
- · Interface methods are always public and abstract.
- · Any variable defined in an interface must be a public constant.
Example:
interface Foo {
int BAR = 42;
void go();
}
class Zap implements Foo {
public void go() {
BAR = 27; // Since, BAR is
interface constant, value cannot be modified.
}
}
- ·
In this example code, the value of a
constant cannot be changed, once the value has been assigned.
- · The Interface constant is declared, when the assignment happens in the interface.
- · So the implementing class can access it and use it, but as a read-only value.