Java Interface


Java Interface in Java language play a very important role in Project designing. Very much useful for Architects. Interface gives a template of methods from which new classes can be developed easily.

Basically, as every Learner says is, Java does not support multiple inheritance and also pointers. Not supporting multiple inheritance is a good drawback but used for a different purpose in a different style by Java Designers. Java programming constructs are constructors, methods and classes. Coming to classes there are three types – Concrete class, Abstract class and Interface.

What is Java Interface?

Java Interface is not completely new to a Learner. It is after all, a special variation of abstract class where all methods should be abstract. Moreover, Java wants to achieve multiple inheritance through interfaces. Infact, interface is a keyword and to inherit an interface, we use implements keyword and not usual extends keyword.

Let us jump into an Example then we discuss further of Java Interface.

interface LandAnimal
{
  public abstract void walk();
}
interface WaterAnimal
{
  public abstract void swim();
}
public class Tortoise implements LandAnimal, WaterAnimal
{
  public void walk()
  {
    System.out.println("Tortoise walks on land");
  }
  public void swim()
  {
    System.out.println("Tortoise swims in water");
  }
  public static void main(String args[])
  {
    Tortoise t1 = new Tortoise();
    t1.walk();
    t1.swim();
  }
}

Java InterfaceOutput Screenshot on Java Interface

LandAnimal and WaterAnimal are two interfaces having different properties. Tortoise would like to have both properties and inherits both interfaces. Observe, for inheritance, we used implements keyword. After implements there are two interfaces. It is an example of multiple inheritance where one class inherits two. Java supports multiple inheritance through interfaces. This style of multiple inheritance is not possible with concrete classes and abstract classes.

Finally, to say

1. Java supports multiple inheritance through interfaces.
2. To inform the compiler that we are using interfaces, write implements keyword.
3. After implements there should be interfaces only but can be any number.

Following links of this same Web site give you more indepth knowledge of Interfaces

1. Interfaces more Explanation and more Examples: Java Interfaces Multiple Inheritance
2. Inheritance in between Interfaces: Java Multiple Interfaces Inheritance
3. Inheriting both Classes and Interfaces: Java Multiple Classes Inheritance

Leave a Comment

Your email address will not be published.