Details of the interface interface example in java

  • 2020-06-15 08:55:42
  • OfStack

interface interface example in java

Interfaces: The Java interface is a collection of method representations, but does not implement specific methods in the interface.

Features of the java interface are as follows:

1. java interface cannot be instantiated
2. The declared members in the java interface are automatically set to public, so there are no private members
3. The implementation of the method cannot appear in the java interface.
To implement an interface, you must implement all the methods defined in it.

Let's look at a case of implementing an interface:


 



package hello;   interface competer{ // Defines the interface void set_compt(int com); void print_compt_information(); } class bj implements competer{ // Interface implementation int com ; public void set_compt(int com) { this.com = com ; // System.out.println(" port " + com); } public void print_compt_information() { System.out.println(" port " + com); System.out.println(" The notebook "); } } class taishi implements competer{ int com ; public void set_compt(int com) { this.com = com ; //System.out.println(" port " + com); } public void print_compt_information() { System.out.println(" port " + com); System.out.println(" desktop "); } } public class inter { public static void main(String[] args) { taishi com = new taishi(); bj bjj = new bj(); com.set_compt(100); bjj.set_compt(120); com.print_compt_information(); bjj.print_compt_information(); } }

Operation results:


 port 100
 desktop 
 port 120
 The notebook 

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: