Java Interface example

  1. /*
  2. Java Interface example.
  3. This Java Interface example describes how interface is defined and
  4. being used in Java language.
  5.  
  6. Syntax of defining java interface is,
  7. <modifier> interface <interface-name>{
  8.   //members and methods()
  9. }
  10. */
  11.  
  12. //declare an interface
  13. interface IntExample{
  14.  
  15. /*
  16.   Syntax to declare method in java interface is,
  17.   <modifier> <return-type> methodName(<optional-parameters>);
  18.   IMPORTANT : Methods declared in the interface are implicitly public and abstract.
  19.   */
  20.  
  21. public void sayHello();
  22. }
  23. }
  24. /*
  25. Classes are extended while interfaces are implemented.
  26. To implement an interface use implements keyword.
  27. IMPORTANT : A class can extend only one other class, while it
  28. can implement n number of interfaces.
  29. */
  30.  
  31. public class JavaInterfaceExample implements IntExample{
  32. /*
  33.   We have to define the method declared in implemented interface,
  34.   or else we have to declare the implementing class as abstract class.
  35.   */
  36.  
  37. public void sayHello(){
  38. System.out.println("Hello Visitor !");
  39. }
  40.  
  41. public static void main(String args[]){
  42. //create object of the class
  43. JavaInterfaceExample javaInterfaceExample = new JavaInterfaceExample();
  44. //invoke sayHello(), declared in IntExample interface.
  45. javaInterfaceExample.sayHello();
  46. }
  47. }
  48.  
  49. /*
  50. OUTPUT of the above given Java Interface example would be :
  51. Hello Visitor !
  52. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Umair saying thx

Well thx for this :)

too many comment

too many comment can't see the program...

it's possible or not

  1.  
  2. class A implements IntfA
  3. {public
  4. int a;
  5. A()
  6. {this(10);
  7. }
  8.  
  9. A(int x)
  10. {a=x;
  11. }
  12.  
  13. public String MethodofIntfA()
  14. {String s="mehul";
  15. return s;
  16. }
  17. public void methodofA()
  18. {System.out.println(" Method Of A " +a);
  19. }
  20. }
  21.  
  22.  
  23. interface IntfA extends IntfB
  24. {
  25. //constant can be declare;;;;;;;;;;;;;;;;;;;; how use it's
  26. public String MethodofIntfA();
  27. //public String MethodofIntfA1();
  28. }
  29.  
  30. public interface IntfB
  31. {//constant can be declare;;;;;;;;;;;;;;;;;;;; how use it's
  32. public String MethodofIntfB();
  33. //public String MethodofIntfA1();
  34. }

is it possible or not

class must implement the method of intfB

hahh??

..wahh..!!! its so hard..

about the interface..................

  1. class A implements IntfA,IntfB
  2. {
  3. public int a;
  4. A()
  5. {this(10);
  6. }
  7.  
  8. A(int x)
  9. {a=x;
  10. }
  11.  
  12. public String MethodofIntfA()
  13. {
  14. String s="Method-A";
  15. // System.out.println(s);
  16. return s;
  17. }
  18. public void methodofA()
  19. {
  20. System.out.println(" Method Of A " +a);
  21. }
  22. public String MethodofIntfB()
  23. {
  24. String s2="Method-B";
  25. return s2;
  26. }
  27.  
  28. public static void main(String args[])
  29. {
  30. A a1=new A();
  31.  
  32. String s1=new String();
  33. String s2=new String();
  34. s1=a1.MethodofIntfA();
  35. s2=a1.MethodofIntfB();
  36. a1.methodofA();
  37. System.out.println("");
  38. System.out.println("Method -IntfA"+s1);
  39. System.out.println("");
  40. System.out.println("Method -IntfB"+s2);
  41.  
  42. }
  43. }
  44.  
  45.  
  46. interface IntfA extends IntfB
  47. {
  48. //constant can be declare;;;;;;;;;;;;;;;;;;;; how use it's
  49. public String MethodofIntfA();
  50. //public String MethodofIntfA1();
  51. }
  52.  
  53. interface IntfB
  54. {//constant can be declare;;;;;;;;;;;;;;;;;;;; how use it's
  55. public String MethodofIntfB();
  56. //public String MethodofIntfA1();
  57. }
  58.  
  59. /*****************/
  60. You will have to implement IntfB interface in the Class A
  61. and i will give you some changes. i hope your query is solved.
  62. /*************************/
  63. output
  64. /**************************/
  65.  
  66. Method Of A 10
  67.  
  68. Method -IntfAMethod-A
  69.  
  70. Method -IntfBMethod-B
  71.  
  72. /**************************/

comments.

those comments r helpful.......

slight modification in ur

slight modification in ur answer

class A no need to implement the interface IntfB because it is already extended by the interface IntfA. So,Class A can get all methods of interface IntfA and IntfB by implementing IntfA itself.

isn't it?


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!