INTERFACE

1
2
3
4
5
6
interface Interface
    {
        void  test1();
        abstract  void test2();
        public  abstract void test3();
    }

O/P: Compile successful .

Note :Inside a interface all methods are Abstract & Public by default.

Ex.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
interface Interface1
    {
        void  test1();
    }
interface Interface2
    {
        void  test2();
    }
interface Interface3  extends Interface1,Interface2
    {
        void  test3();
    }
class A  implements Interface3 /* All  methods in the interface must be implemented within a class.*/
    {
        public  void test1()
            {
                System.out.println("test1");
            }
        public  void test2()
            {
                System.out.println("test2");
            }
        public  void test3()
            {
                System.out.println("test3");
            }
    }
class InterfaceMain
    {
        public  static void main(String[] args)
            {
                A  d1 = new A();
                d1.test1();
                d1.test2();
                d1.test3();
                System.out.println("done");
            }
    }

An interface in the Java programming language is an abstract type that is used to specify an interface classes must implement. Interfaces are declared using the interface keyword, and may only contain signature and constant declarations (variable declarations that are declared to be both static and final. An interface may never contain method definitions.

Interfaces cannot be instantiated, but rather are implemented.A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.

As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.

O/P:    

java interface

Note: An interface can't inherit from a class in Java but only we can implements.

Ex.3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
interface Interface1
    {
        void  test1();
    }
interface Interface2
    {
        void  test2();
    }
class Test
    {
        void  test3()
            {
                System.out.println("test3");
            }
    }
class  A extends Test implements  Interface1,Interface2     
    {
        public  void test1()
            {
                System.out.println("test1");
            }
        public  void test2()
            {
                System.out.println("test2");
            }
    }
class Main
    {
        public  static void main(String[] args)
            {
                A  h1 = new A();
                h1.test1();
                h1.test2();
                h1.test3();
                System.out.println("done");
            }
    }

O/P:   

java interface

Note: You may have the extend-ing first before the implement-ing .