Java Interface
An interface is a fully abstract class. It includes a group of
abstract methods (methods without a body).
We use the interface keyword to create an interface in Java.
interface Language {
public void getType();
public void getVersion();
}
Implementing an Interface
Like abstract classes, we cannot create objects of
interfaces.
To use an interface, other classes must implement
it. We use the implements keyword to implement
an interface.
interface Polygon {
void getArea(int length, int breadth);
}
// implement the Polygon interface
class Rectangle implements Polygon {
// implementation of abstract method
public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length
* breadth));
}
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
} The area of the rectangle is 30
// create an interface
interface Language {
void getName(String name);
}
// class implements interface
class ProgrammingLanguage implements Language {
// implementation of abstract method
public void getName(String name) {
System.out.println("Programming Language: " + name);
} }
class Main {
public static void main(String[] args) {
ProgrammingLanguage language = new
ProgrammingLanguage();
language.getName("Java");
}
}
Implementing Multiple Interfaces
In Java, a class can also implement multiple interfaces. For
example,
interface A {
// members of A
}
interface B {
// members of B
}
class C implements A, B {
// abstract members of A
// abstract members of B
}
Extending an Interface
interface Line {
// members of Line interface
}
// extending interface
interface Polygon extends Line {
// members of Polygon interface
// members of Line interface
}
Extending Multiple Interfaces
interface A {
...
}
interface B {
...
}
interface C extends A, B {
...
}
Interfaces are also used to achieve multiple inheritance in
Java.
interface Line {
…
}
interface Polygon {
…
}
class Rectangle implements Line, Polygon {
…
}
interface Language {
// by default public static final
String type = "programming language";
// by default public
void getName();
}
All the methods inside an interface are implicitly public and
all fields are implicitly public static final
default methods in Java Interfaces
With the release of Java 8, we can now add methods with
implementation inside an interface. These methods are
called default methods.
To declare default methods inside interfaces, we use the
default keyword. For example,
public default void getSides() {
// body of getSides()
}
interface Polygon { // implements the interface
void getArea(); class Square implements Polygon {
// default method
public void getArea() {
int length = 5;
default void getSides() {
int area = length * length;
System.out.println("I can get sides
of a polygon."); System.out.println("The area of
}}
the square is " + area);
// implements the interface
}
}
class Rectangle implements Polygon
{ class Main {
public void getArea() { public static void main(String[]
int length = 6;
args) {
// create an object of Rectangle
int breadth = 5;
Rectangle r1 = new Rectangle();
int area = length * breadth;
r1.getArea();
System.out.println("The area of the
rectangle is " + area);
r1.getSides();
}
// create an object of Square
// overrides the getSides()
Square s1 = new Square();
public void getSides() {
s1.getArea();
System.out.println("I have 4
s1.getSides(); The area of the rectangle is 30
sides."); I have 4 sides.
}
} The area of the square is 25
} I can get sides of a polygon.
}
interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}
class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
Java Varargs
Varargs is a short name for variable arguments.
In Java, an argument of a method can accept arbitrary
number of values. This argument that can accept variable
number of values is called varargs.
accessModifier methodName(datatype… arg) {
// method body
}
… can take any number of arguments
class NoVararg {
public int sumNumber(int a, int b){
return a+b;
}
public int sumNumber(int a, int b, int c){
return a+b+c;
}
public static void main( String[] args ) {
NoVararg obj = new NoVararg();
System.out.println(obj.sumNumber(1, 2));
System.out.println(obj.sumNumber(1, 2, 3));
}
}
// To use the sqrt function // calculate the area of a triangle
import java.lang.Math; public void getArea() {
interface Polygon {
s = (double) (a + b + c)/2;
void getArea();
area = Math.sqrt(s*(s-a)*(s-
// calculate the perimeter of a Polygon
default void getPerimeter(int... sides) {
b)*(s-c));
int perimeter = 0; System.out.println("Area: " +
for (int side: sides) { area);
perimeter += side; }
} }
System.out.println("Perimeter: " + class MainInterface {
perimeter);
public static void main(String[]
}
args) {
}
class Triangle implements Polygon {
Triangle t1 = new Triangle(2, 3,
private int a, b, c; 4);
private double s, area; // calls the method of the Triangle
// initializing sides of a triangle class
Triangle(int a, int b, int c) { t1.getArea();
this.a = a; // calls the method of Polygon
this.b = b; t1.getPerimeter(2, 3, 4);
this.c = c; }
s = 0;
}
}