INTERFACE
Interface is similar to a class but only contains abstract
methods (methods without a body)
Interface is also called as blueprint of class
Interface is used to achieve 100% abstraction because
in abstraction using a non-abstract methods its not
consider as 100% abstraction so using a interface we
can only use abstract method in interface
In interface we can't create object for interface same
as abstraction
To access abstract method in interface need to
override to interface child class and creating an
runtime object (runtime polymorphism) we can access
abstract method
We can't use non-abstract method in interface
(methods with body)
Java doesn't support multiple inheritance with java
classes, but a class can implement multiple interfaces.
Advantage of using interface a class can be implements
multiple interfaces
Disadvantage of using interface before java 8,
interfaces couldn't have non-abstract(method with
body) after java 8 we can use static and default
methods(method with body) in interface
In interface after java 8, only we can have public, static
and final constants (variables and methods)
Sample program for an interface using a shapes:
package javaprogram;
// for create java interface we need to use
interface keyword
interface shapes
{
// we can create n of number abstract methods
in interface
void square(); // abstract method square
void rectangle(); // abstract method rectangle
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
class shapesDefineSize implements shapes
{
public void square()
{
// Area of square
int width=5; int height=5;
int squareArea=width*height;
[Link]("Area of square
"+squareArea);
// using a width or height multiple to
side value get perimeter
// Perimeter of square
int squarePerimeter=width*4;
[Link]("Perimeter of square
"+squarePerimeter);
}
public void rectangle()
{
// Area of rectangle
int length=4; int width=6;
int rectangleArea=length*width;
[Link]("Area of rectangle
"+rectangleArea);
// Perimeter of square
int size=2;
int
rectanglePerimeter=size*(length+width);
[Link]("Perimeter of rectangle
"+rectanglePerimeter);
}
}
public class TopicInterface{
public static void main(String[] args) {
// create object of runtime(runtime
polymorphism)
shapes shapessize=new shapesDefineSize();
[Link]();
[Link]();
}
}
(After java 8)Using a non-abstract methods (method with
body default and static) and variable as final in interface
package javaprogram;
// for create java interface we need to use
interface keyword
interface shapes
{
void circle(); // abstract method
// in interface for default method we need to
use default keyword
default void defaultmethod() // default method
{
[Link]("interface shape default
method");
}
static void staticmethod() // static method
{
[Link]("interface shape static
method");
}
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
class shapesDefineSize implements shapes
{
public void circle()
{
// Area of circle
int radius=3; int mathOfCircle=3;
int
circleArea=radius*radius*mathOfCircle;
[Link]("Area of circle
"+circleArea);
// Perimeter of circle
int size=2;
int
PerimeterCircle=size*radius*mathOfCircle;
[Link]("Area of circle
"+PerimeterCircle);
}
}
public class TopicInterface{
public static void main(String[] args) {
// create object of runtime(runtime
polymorphism)
shapes shapessize=new shapesDefineSize();
[Link](); // calling abstract
method of shapes interface
[Link]();// calling
default method of shapes interface
[Link]();// calling static
method of shapes interface
}
}
We can use final variable in java interface
package javaprogram;
// for create java interface we need to use
interface keyword
interface Numbers
{
int a=100; // the variable as automatically
define as public, static and final
void add(); // abstract method
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
class adds implements Numbers
{
public void add()
{
int b=200;
// We didn't use any object or static or
final but its access direct a variable
int addition=a+b;
[Link](addition);
}
}
public class TopicInterface{
public static void main(String[] args) {
// create object of runtime(runtime
polymorphism)
Numbers addValue=new adds();
[Link](); // calling abstract method
}
}
Using a multiple inheritance in java interface
package javaprogram;
// for create java interface we need to use
interface keyword
interface GotWhatsappMessage
{
void message();
}
interface ViewWhatsappMessage
{
void view();
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
// using multiple inheritance in java
class notification implements
GotWhatsappMessage,ViewWhatsappMessage
{
public void message()
{
[Link]("got message in
whatsapp");
}
public void view()
{
[Link]("view message in
whatsapp");
}
}
public class TopicInterface{
public static void main(String[] args) {
// create object of runtime(runtime
polymorphism)
GotWhatsappMessage gotmessage=new
notification();
[Link](); // calling
GotWhatsappMessage abstract method
ViewWhatsappMessage viewmessage=new
notification();
[Link]();// calling
ViewWhatsappMessage abstract method
}
}
Creating an multiple interface in java we can connect
interfaces using extends keyword for create single object
package javaprogram;
// for create java interface we need to use
interface keyword
interface GotWhatsappMessage
{
void message();
}
// connect one interface to another interface using
a extends keyword
interface ViewWhatsappMessage extends
GotWhatsappMessage
{
void view();
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
// using multiple inheritance in java
class notification implements
GotWhatsappMessage,ViewWhatsappMessage
{
public void message()
{
[Link]("got message in
whatsapp");
}
public void view()
{
[Link]("view message in
whatsapp");
}
}
public class TopicInterface{
public static void main(String[] args) {
// create object of runtime(runtime
polymorphism)
ViewWhatsappMessage viewmessage=new
notification();
[Link]();// calling
GotWhatsappMessage abstract method
[Link]();// calling
ViewWhatsappMessage abstract method
}
}