Interface in java
The interface keyword is used to declare an interface. An interface is a blueprint of a class.
- We can't create an instance of interface .
- By using interface , we can achieve abstraction in java
- By using interface , we can implement the functionality of multiple inheritance.
- It does not contain any constructor.
- All the methods are abstract method in interface.
- An interface can't be extends by a class but extends by interface .
- An interface can't be implemented by interface but implemented by a class.
Example :
interface Car{
float getAmount();
}
class Audi implements Car{
public float getAmount(){return 2.5f;}
}
class Bmw implements Car{
public float getAmount(){return 80.3f;}
}
class Test{
public static void main(String args[]){
Car b;
b=new Audi();
System.out.println("Audi Price is: "+b.getAmount()+"Cr");
b=new Bmw();
System.out.println("BMW Price is: "+b.getAmount()+"L");
}
}
Output :
Audi Price is: 2.5Cr
BMW Price is: 80.3L
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Comments
Post a Comment