Header Ads

Interface

 



What are Interfaces ?

Multiple inheritances are not supported by the Java. Multiple inheritance is implemented by using concept called as Interface. Interface is like a class. Interface contains methods and variables but in different manner.

Interface only contains abstract methods and final fields (variables). This means that interfaces do not specify any to implement these methods and data fields are declared as a constant. i.e. Variables declared inside interfaces are by default public, final and static.

Properties of Interfaces 

1. An interface can contain either constants variables or abstract methods or both.
2. Methods are public and abstract by default.
3. Constants in an interface are always public, static and final by default.
4. Interface is defined using a keyword interface.
5. The name of the interface must be different from any other class in the same package.
6. Since the variables are final and static, they must be initialized and cannot be altered
7. Only public access modifier is can be used for an interface.
8. Interface only defines the signature of a method; the implementation (method definition) is done by the class which implements the interface.
9. All methods in the interface should be implemented by the class implementing the interface
10. Constants of interface can be accessed without using dot notation.
11. Interface do not contain any implementations and can permit multiple interface inheritance
12. Interface cannot be instantiated.
13. When a method of an interface is implemented in a class it should always be public
14. Interface methods cannot be declared static.
15. Interface does not implement another interface, but they extend another interface.
16. If a class implements an interface, then reference to objects of this class and its subclass can be assigned to a variable of this interface type.

Syntax

syntax of interface

Note:

Methods declared are abstract methods. An interface contains constant values & method. Declarations The difference between class and interface is that the methods in an interface are only declared but not implemented. Interface adds most of the functionality that is required for many applications which would normally reset to using multiple inheritances.

It is Responsibility of any class that implements the derived interface to define all the methods. When interface extends two or more interfaces, they are separated by commas. Interface cannot extend classes.

Example:

   interface volume
   {
        final static float pi = 3.142f;
        float compute(float x, float b, float h);
        void show();
    }

Extending Interface

Like classes, interface can also be extended. That is, an interface can be sub interfaced from other interfacesThe new sub-interface will intent all the members of super-interface in the manners similar to sub-class. This is achieved using the keyword extends.

      interface name2 extends namel
      {
           //body of name2
       }

Example

            interface ItemConstants
            {
                 int code = 1001;
                 String name = "fan";
             }
             interface ItemMethods
             {
                  void display();
             }
             interface Item extends ItemConstants, ItemMethods
             {   ……………
                  ……………
             }

Implementation of Interfaces: Interface are used as "superclasses" whose properties are inherited by classes.

Syntax:

   I) class classname implements interfacename
   
           {
                 //body of classname;
            }

   II) class classname extends superclass implements interface1, interface2

           {
                  // body of classname
            }

In above syntax, class can extend another class while implementing interface.

Q. WAP to implement concept of interface.

Code

interface Area 
{
    final flat pi=3.14f;
    float compute(float x,float y);
} 
class Rectangle implements area
{ 
   public float compute(float x, float y) 
   { 
     return(x*y); 
   } 
} 
class Circle implements area 
{ 
   public float compute(float x,float y) 
   {
     return(pi*x*x); 
   } 
} 
class Interfacetest 
{ 
   public static void main(String args[]) 
   { 
     Area ref; 
     ref = new Rectangle(); 
     System.out.println(" area of Rectangle="+ref.compute(2.2,3.2)); 
     ref=new Circle(); 
     System.out.println(" area of Circle="+ref.compute(2.5,2)); 
   }
} 
Q.  Difference between Abstract Class and Interface 

Difference between Abstract Class and Interface

Q. Explain difference between Concrete Class, Abstract class & Interface

difference between Concrete Class, Abstract class & Interface

No comments

Powered by Blogger.