Java Implements


In Java implements is a keyword. Where to use implements keyword in Java coding? We know Java does not support multiple inheritance. That is, one class cannot extend or inherit two classes. That is, following code does not work.

class Bird { }
class Animal { }
public class Ostrich extends Bird, Animal { }

Infact, not supporting mmltiple inheritance is a drawback of Java. Just to support multiple inheritance, Java introduced interfaces. One property of interface is it should have all abstract methods. Any class would like to inherit the interface, should use implements keyword instead of general extends keyword. Now let us see how to use Java implements keyword with interfaces.

Example on Java implements
interface Animal                                // observe, interface keyword usage              
{
  public abstract void walk();                  // interface should have only abstract methods
}
interface Bird
{
  public abstract void fly();
}
public class Ostrich implements Bird, Animal   // observe, Java implements with interfaces
{
  public void walk()			       // overide abstract method of Animal
  {
    System.out.println("Animal walks");
  }
  public void fly()		               // override abstract method of Bird
  {
    System.out.println("Birds fly");
  }
  public static void main(String args[])
  {
    Osrtrich o1 = new Ostrich();
    o1.walk();
    o1.fly();
  }
}


Java implements
Output screenshot of Java implements

Observe, in the above code, Ostrich inherits two interfaces using implements keyword and not extends. After Java implements there should be interfaces only of any number.

To know more on inheritance with interfaces and a table on extends vs implements refer Java Multiple Interfaces Inheritance.

Leave a Comment

Your email address will not be published.