MainMenu

Home Java Overview Maven Tutorials

Sunday 15 January 2017

Interface in Java with example

For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


Interface in Java with example


Video for this tutorial : CLICK HERE




An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
An interface is similar to a class in the following ways:
· An interface can contain any number of methods.
· An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
· The bytecode of an interface appears in a .class file.
· Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including:
· You cannot instantiate an interface.
· An interface does not contain any constructors.
· All of the methods in an interface are abstract.
· An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
· An interface is not extended by a class; it is implemented by a class.
· An interface can extend multiple interfaces.
“interface is a pure abstract class. They are syntactically similar to classes, but you cannot create instance of an interface and their methods are declared without any body. Interface is used to achieve complete abstraction in java.”

Why Interface provide 100% abstraction ?

Note :
I would like to explain the difference in non programming language.
Assume that you have TV and a Remote. By Remote, you can operate with TV.
Remote had On and Off buttons. When you click on On button, TV will switch on. But you don't know internals of On button implementation . Same is the case with Off button, which switches off TV.
Now Remote is an interface and TV is an implementation object. Map same concepts in java programming language and you will get more clarity.
Remote is interface with On and Off methods. TV implements Remote interface.
Interface IS already 100% abstract.
You can't say it's not 100% abstract because We are writing code in class which implements that interface. By any means you have to write code somewhere to do some task.
If Class C implements I, Then
You can call all methods of C using I without exposing any details of C. So I helped to hide implementation details of C.

Since Java 8, there is an update, by which we can have static and default methods in an interface.
and Since Java 9, we can also include private methods in an interface.


Interface Example :


interface Maths
{
double pi = 3.14;
public double area(double r);
}
class Circle implements Maths
{
public double area(double r)
{
return pi*r*r;
}
}
class Sphere implements Maths
{
public double area(double r)
{
return 4*pi*r*r;
}
}
public class Main
{
public staic void main(String args[])
{
double r = 5;
Maths obj1 = new Circle();
Maths obj2 = new Sphere();
System.out.println("the Circle are is :" +obj1.area(r));
System.out.println("the Sphere are is :" +obj2.area(r));
}
}
Output :
the Circle are is :78.5
the Sphere are is :314.0

Tags:

Interface in java with example



Tags :-

What is interface in java?

Example of interface in java?

No comments:

Post a Comment