Introduction:-
Interface in Java in this tutorial is explain about What is Interface in Java and also explain about types of interface and how to defined interface with explanation and given example of Java Programming.
What is interface in Java :-
Interface in Java is blueprints of a class. It is contains only abstract methods and not method body. Interface is call in his subclass by the use of implements keyword and method of interface is overridden in sub class. Interface is support the Multiple Inheritance. It can be used to be loose coupling.
Syntax:-
interface interface_name{
...............
...............
}
Most Read:- If...else statement in Java
Use of interface in Java:-
1. Interface is contains always public abstract method. if you create method inside the interface then compiler is automatically always add public abstract before the method of interface.
interface interface_name{
public abstract void display();
...............
}
2. Interface is containts field or variables that is always public static final. compiler add public static final before the declare variable of interface.
interface interface_name{
void display();
public static final int 10;
}
3. After updates in Java 8 version, We create a normal method inside the interface using default keyword.
interface interface_name{
public static final int 10;
default void display();
}
4. After update in Java 9 version, We create a private methods inside the Interface.
interface interface_name{
public static final int 10;
private void display();
}
Most Read:- Introduction of Android
Example of Interface :-
interface Collage{
void Student_Enter_Details();
void Student_Details_Print();
}
class MainExample implements Collage{
int mRollno; String mName; double mMarks;
public void Student_Enter_Details() {
Scanner s = new Scanner(System.in);
mRollno = s.nextInt();
mName = s.nextLine();
mMarks = s.next.Double();
}
public void Student_Details_Print(){
System.out.println(mRollno+ " "+mName+" "+mMarks);
}
public static void main(String [] args){
MainExample main = new MainExample();
main.Student_Enter_Details();
main.Student_Details_Print();
}
}
Most Read:- Access Modifier use in Java
0 Comments
Hello Friends, Please comment and share and do not write spam messages