An interface is a blueprint of a class. It has static constants and abstract methods.The interface is a mechanism to achieve abstraction injava.There can be only abstract methods in the interface.
It is used to achieve fully abstraction and multiple inheritance in Java.
Why use Interface?
There are mainly three reasons to use interface. They are given below.
>1.It is used to achieve fully abstraction.
>2.By interface,we can support the functionality of multiple inheritance.
>3.It can be used to achieve loose coupling.
Note:The java compiler converts methods of interface as public and abstract, data members as public,final and static bydefault.
Example of Interface:
Befor compilation code (Bike.java) is:
interface Bike {
int speed=60;
void run();
}
After compilation code (Bike.class) is:
interface Bike {
public static final int speed=60;
public abstract run();
}
Simple Example of Interface:
In this exmple,Bike interface have only one method,its implemenation is provided in the Test class.
interface Bike{
void run();
}
class Test implements Bike
{
public void print(){ System.out.println("Hello"); }
public static void main(String []args){
Test t = new Test();
t.run();
}
}
Output: Hello
How Multiple Inheritance is supported by interface ?
Exampel:
interface Bike{
void run();
}
interface Car{
void speed();
}
class Test implements Bike, Car {
public void run() { System.out.println("Running..."); }
public void speed() { System.out.println("Speeding..."); }
public static void main(String []args){
Test t = new Test();
t.run();
t.speed();
}
}
Output:Running...
Speeding...
Note: In the above example there is two interface Bike and Car and has two method.If we want access those two methods we can easily implements both interfaceand use both method.But it is only possible in interface case.
If these two methods are in two different class then we can't access both methods
because we can't extends two class so this senario is possible by interface
thats why we say Multiple Inheritance is achieved by Interfacce.
Note: We can implements two or more interface but we can't extends more than one class.
Another Example of Multiple Inheritance by Interface:
interface Bike{
void run();
}
interface Car{
void run();
}
class Test implements Bike,car{
public void run() { System.out.println("Running...");}
public static void main(String []args){
Test t = new Test();
t.run();
}
}
Output: Running...
Note: As you can see in the above example,Bike and car interface have same methods but its
implementation is provided by class Test, so there is no ambiguity.
Note: A class implements interface but One interface extends another interface also.
interface Bike{
void run();
}
interface Car extends Bike{
void speed();
}
class Test implements Car{
public void run() {System.out.println("running"); }
public void speed() {System.out.println("speeding"); }
public static void main(String []args){
Test t= new Test();
t.run();
t.speed();
}
}
Output:running
speeding
What is marker or tagged interface ?
An interface that have no member is known as marker or tagged interface.For example: Serializable,
Cloneable, Remote etc.They are used to provide some essential information to the JVM so that JVM may perform some useful operation.
Note: An interface can have another interface i.e. known as nested interface.We will learn it
in detail in the nested classes post.
For example:
interface Bike{
void run();
interface runningCar{
void running();
}
}
Note:A Interface which has only one method is known as Functional Interface. For example Runnable Interface has only one method as public void run().
No comments:
Post a Comment