Java Interface Multiple Inheritance


An interface in Java gives a template of methods where the implementation or body for the methods is missing. The body or implementation is given by the sub classes in the way they think most appropriate.

For example, I would like to prepare curry to take in my dinner. Imagine there is an interface as follows.

public interface Curry
{
  public abstract void purchaseVegetables();
  // some more methods exist that show how to make curry with vegetables
}

The interface says to purchase vegetables. Which vegetables from where and at what time? Nothing is given by the interface Curry. Let us see the subclass.

public class WantedCurry implements Curry
{
  public void purchaseVegetables()
  {
    System.out.println("Purchase Cabbage while returning from office");
  }                                         // main() method follows later         
}

Because I like cabbage or cabbage is cheaper in price, I preferred cabbage. If you implement the Curry interface and override purchaseVegetables() method, you purchase your fancy of vegetables.

Interface tells you what to do but now how to do. How to do is the job of subclass which implements the interface. Following programs illustrates.

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

A. Basic Programs

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

B. Advanced topics on Java Interfaces

1. Casting objects including interfaces
2. Abstract class constructor & Interface abstract
3. Abstract Static Method
4. Dynamic Polymorphism with classes and Java interfaces
5. instanceof Interface Java

Leave a Comment

Your email address will not be published.