Interfacing in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harish4java
    New Member
    • Aug 2008
    • 1

    Interfacing in java

    hi everybody am new to work with java. So please do help to be a successful developer in java.....
    My first question is How java can implement interface concept in the place of inheritance? And please tell the steps to implement interface concept?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If a class C implements an interface I it signs a contract saying: you can treat
    me as an I instead of a C. It adds another 'face' to the class. Implmenting a
    interface is easy; e.g.

    [code=java]
    // the interface:
    public interface I
    public int aMethod();
    public int anotherMethod() ;
    }
    // the implementing class:
    public class C implements I {
    // complete implementation of I
    public int aMethod() { return 42; }
    public int anotherMethod() { return 54; }
    }
    [/code]

    If a class says it implements an interface as in "implements I" but it doesn't
    implement all of the methods of the interface, the class must be abstract so
    that a derived class can complete the implementation of the interface:

    [code=java]
    public abstract class C implements I {
    public int aMethod() { return 42; } // partial implementation of I
    // no implementation of anotherMethod()
    }
    public class Derived extends C {
    public int anotherMethod() { return 54; } // completes implementation of I
    }
    [/code]

    This all is completely explained in your textbook; read it.

    kind regards,

    Jos

    Comment

    Working...