• Interface in Java

    INTERFACE 

    Interface can be used to fully abstract a class’ interface from its implementation. With interface we can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. Once an interface is defined any number of classes can implement it and also one class can implement any number of interfaces.

    • POLYMORPHISM

    To implement an interface , a class must provide the complete set of methods required by the interface .However , each class is free to determine the detail of its own implementation . Thus , interface allows programmers to utilise the “One inetface , Multiple methods ” aspect of polymorphism .

    • CHARACTERISTICS OF INTERFACE :

    Interfaces are designed to support dynamic method resolution at runtime . In order for a method to be called from one class to another class. Both classes need to be present at compile time so the java compiles can check to ensure that the method signatures are compatible. In a system like this functionality gets pushed up higher and higher in the class hierarchy so that mechanisms are available to more and more subclass . This results in a static and non existansible classing environment . Interfaces are designed to avoid this problem by disconnecting the definition of a method or a set of methods from the inheritance hierarchy.

    •  Implementing a interface : 

    Once an interface is defined any number of classes can implement it and also one class can implement any number of interfaces. To implement an interface , we include the implements clause in a class definition and then create the methods required by interface . The general form of a class that includes the implements clause is shown below :
    class classname [extends superclass] [implements interface [,interface...]]
    {
    // class-body
    }

    EXAMPLE:


    HOW TO DECLARE VARIABLES IN AN INTERFACE ?


    When we include ( implements ) an interface in a class all those variable names will be in the scope as constants . If an interface contains no methods then any class that implements such an interface does not actually implement anything. It is as if that class were importing the constant field into the class name space as final variables.

     CAN INTERFACE BE EXTENDED ?

    One interface can inherit another by using the extends keyword . This is syntactically similar to inheriting classes . When a class implements an interface that inherits another interface must provide implementations for all the methods required by the interface inheritance chain.

    interface Geo1
    {
    void cir (Int a , int b )
    }
    interface Geo2 extents Geo1
    {
    void area (int a , int b )
    }
    class Rectangle implements Geo2
    {
    public void area (int a , int b)
    {
    int c = a*b ;
    System.out.println(“Area”+c);
    }
    public void area ( int a , int b )
    {
    int c = 2*(a+b);
    System.out.println(“Area”+c);
    }
    }



  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!