Java Interfaces Tutorial


Many think that abstract methods are waste and interfaces are still more waste. If you understand correctly, you find their immense usage. Let us see one on Java Interfaces.

Suppose I would like to construct a house. I take the help of Java interfaces as follows.

public interface Structure
{
  public abstract void use53GradePortlandCement();
  // some more methods exist that help to construct a house
}

Observe, the method use53GradePortlandCement() does not have a body, at least empty braces. These type methods without body are known as abstract methods. I want to implement the interface to may class as follows.

public class RaoHouse implements Structure
{
  public void use53GradePortlandCement()
  {
    System.out.println("Use Ultratech 53 grade cement");
  }
}

My class RaoHouse implements the interface Structure and overrides the abstract method in the way it is suitable for it. Because I preferred Ultratech cement because the shop owner gives me the credit. It is 53 grade cement only as required by the interface. If you implement the interface, you will override the abstract method in your own way like Orient Gold cement as it is available plenty in your area at that time.

Java Interfaces are like blue print for a house to construct. Using the blue print we construct the house. Blue print (paper) is not itself a house. Similarly, interface gives a blue print (template) of methods from which new classes can be developed easily.

Following links of this same Web site gives you indepth knowledge of Interfaces

A. Basic Programs

1. Interfaces – Partial implementation of Multiple Inheritance
2. Play with Implements
3. Extends Implements

Leave a Comment

Your email address will not be published.