Interface
What is interface:
- Interface is similar to class.it is a collection of abstract method.
- Interface dont have defination.
- A class can implements an interface.thereby inheriting a abstract methods of the interface.
- Abstract methods an interface also contain constants,and default methods,static methods.
- Method body exits only for default methods and static methods.
- Interface can have any number of methods.
- You cant create object for interface.
- Interface dont not have constractor.
- An interface can extend multiple interfaces.
Declaring Interfaces:
The interface keyword is used to declare an interface.
Example:
public interface SampleInterface{
}
properties for interface:
- An interface is a implictily abstract menthod so you no need to declare abstract keyword.
- Methods in an interface are implicitily public.
interface Animal{
public void eat();
public void travel();
}
Implemeting Interface:
- A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
public class Mammal implements Animal{
public void eat(){
System.out.println("Mammal can eat");
}
public void travel(){
System.out.println("Mammal can travel");
}
public int noOfLegs(){
return 0;
}
Public static void main(String args[]){
Mammal mam=new Mammal();
mam.eat();
mam.travel();
}
}
Result:
Mammal can eat.
Mammal can travel.
Please share your feedback about this topic.
0 comments:
Post a Comment