Categories

Interface in JAVA

0

  Interface in java

1. An interface is a type used for accessing the members of some other class instance.

2. Group of abstract methods is called ‘interface’

  •      There is no members confliction in interface because the members functions/methods have only   prototype, that’s why use application of multiple inheritance through interface.
  •     The member that an interface intended to access are declared as it’s abstract method.

syntax for declaration of interface in java :

   interface interface_name

                {

                     // static final variables

                    // abstract and public abstract methods

                }

5.  A class implements an interface using “implements” keyword.

6. When a class implements an interface using it must implement all of its abstract methods otherwise class must abstract.

7. One class may implement multiple interfaces so that instance gets multiple references.

8. One interface may inherit one or one more interface using “extends” keyword.

9. An interface may contain only abstract methods but in addition static final properties ans static inner classes.

10.The Methods in interface are by default abstract, the properties are by default static final , the inner classes are static ans all members are always public.

Lets see the example which satisfies all the above points we have mentioned.

interface A

{

    int sum();       // point 3

}

interface B

{

    int difference();       // point 3

}

interface C extends A,B         // we can it as multiple inheritance ( point 8)

{

   int product();

}

interface D

{

int division();     // point 9

}

class calculate implements C,D         //point 5

{

int fno,sno;

public calculate(int fno,int sno)

{

this fno=fno;

  this.sno=sno;

}

public int sum()

{

  return fno+sno;

}

public int difference()

{

   return fno-sno;

}

public int division()

{

   return fno/sno;

}

public int product()

{

 return fno*sno;

}

} // class calculate close

public class Main()

{

public static void main(String[] args)

{

 Calculate cal=new Calculate(20,10);     // point 7

 A a - cal;                                            //point 1

 B b = cal;

 C c = cal;

 D d = cal;

 int result= a.sum();

 System.out.println(" The sum of two numbers is :"+ result);

 int result= b.difference();

 System.out.println(" The difference of two numbers is :"+ result);

 int result= c.sum();

 System.out.println(" The product of two numbers is :"+ result);

 int result= d.sum();

 System.out.println(" The division of two numbers is :"+ result);

}// main close

} // class close

 

 

OUTPUT :

The sum of two numbers is : 30

The difference two numbers is : 10

The product of two numbers is : 30

The division of two numbers is : 30

 

Finally Save, Compile, Run and Observe the Output.

Note :

  • None of the methods in an interface should be private, protected and static.
  • At compile time , all interface are converted into bytecode( .class) . i.e., in the above example will get A.class, B.class, C.class and D.class apart from “Calculate.class”.

 

Point to Ponder:
1. While designing, how do you choose between Abstract & Interface ?

Answer : Use an abstract class when a template needs to be defined for a group of subclasses
Use interface when role needs to be defined for other classes irrespective of the inheritance tree of the classes

2. Can an interface be instantiated in Java?

Answer : No, an interface can not be instantiated in Java.

 3. Why the methods of interface are public and abstract by default ?

Answer: Interface methods are public since they should be available to third-party vendors to provide implementation. They are abstract as the implementation is left to third party.

4. Can we write a class with in an interface ?

Answer : Yes we can write a class with an interface .

For further discussion, leave a comment below.

Share.

Leave A Reply