Interface Inheritance - Example

A class that inherits both of these interfaces can be declared as:

public class TVDinner implements Edible, Cooked {

public TVDinner(float cal, boolean cooked, float cTemp, float rTemp)

        public float numCalories() {

                return calories; }

        public float getTemp() {

                if(this.isCooked == “true”) return cookedTemp;

                else   return rawTemp; }

        private float calories;

        private boolean isCooked;

        private float cookedTemp, rawTemp; }

This example shows how inheritance is accomplished using interfaces.  Because an interface is not a class, multiple inheritance in Java is accomplished using interfaces.  As this example demonstrates, a single concrete class, TVDinner, can have the properties of being edible and cooked.  If Edible and Cooked were abstract classes, it would not be possible for TVDinner to take on the properties of both.  Notice how TVDinner provides implementations for the methods contained in both interfaces.