Program to Demonstrate use of Inheritance, Polymorphism, interface, method overriding etc

class Food {
    protected String name;
   
    // Setter or mutator
    public void setName(String name) {
        this.name = name;
    }
   
    // Getter or accessor
    public String getName() {
        return name;
    }
}

interface Eatable {
    public abstract String howToEat();
}

class Fruit extends Food {
}

class Vegetable extends Food {
}

class Mango extends Fruit implements Eatable {
    public String howToEat() {
        return "Peel it, slice it and make shake! Humm Yummy!";
    }
}

class Apple extends Fruit implements Eatable {
    public String howToEat() {
        return "Peel and make Apple cider.";
    }
}

class Orange extends Fruit implements Eatable {
    public String howToEat() {
        return "Peel it, juice it and drink it. Healthy!!!";
    }
}

class Tomato extends Vegetable implements Eatable {
    public String howToEat() {
        return "Make soup out of it. Really!!";
    }
}

class Carrot extends Vegetable implements Eatable {
    public String howToEat() {
        return "Grate it and make GAJRELLA!!!";
    }
}


public class MultipleInheri {
    public static void main(String[] args) {
        Object[] objects = { new Apple(), new Orange(), new Mango(), new Tomato(), new Carrot() };
       
        for (int i = 0; i < objects.length; i++) {
            if (objects[i] instanceof Eatable)
                System.out.println(((Eatable)objects[i]).howToEat());
        }
    }
}



Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments