Object Oriented Programming : interface
We can keep a group of methods without any body in an interface.
Usually these are related methods.
We created here one interface games. Inside games we kept outdoor, indoor methods.
Note that here we have used class game_types by using the keyword implements ( not used the keyword extends )
package Pack1;
public class my_interface {
interface games{
public void outdoor();
public void indoor();
}
static class game_types implements games{
public void outdoor() {
System.out.println("Cricket");
}
public void indoor() {
System.out.println("Table Tennis");
}
}
public static void main(String[] args) {
game_types my_game=new game_types();
my_game.outdoor();
my_game.indoor();
}
}
Output is here
Cricket
Table Tennis
We can change the above code like this ( removed static )
package Pack1;
interface games{
public void outdoor();
public void indoor();
}
class game_types implements games{
public void outdoor() {
System.out.println("Cricket");
}
public void indoor() {
System.out.println("Table Tennis");
}
}
public class my_interface {
public static void main(String[] args) {
game_types my_game=new game_types();
my_game.outdoor();
my_game.indoor();
}
}
« Java
This article is written by plus2net.com team.
plus2net.com