Over 500 Magazines for FREE!

Yes! You can subscribe to ALL of them. They will be shipped to your home FREE of cost!
Kindly click here to apply!

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. */

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.......

Post new comment

To combat spam, please enter the code in the image.




Do you have a better example?
We're sure you have hundreds of Java program examples.

Spare some time and submit your java example here even if you think it's too small to contribute.

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




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