Sunday, 12 March 2023

10. The abstract Vegetable class has four subclasses named cabbage, carrot and potato. Write an application that demonstrates how to establish this class hierarchy. Declare one instance variable of type string that indicates the color of a vegetable. Create and display instances of these object. Override the toString() method of object to return a string with the name of the vegetable and its color.



abstract class Vegetable
{
    String color;
    abstract String tostring();
}

class Cabbage extends Vegetable
{
    String tostring()
    {
        color = "green";
        return "cabbage color: "+color;
    }
}

class Carrot extends Vegetable
{
    String tostring()
    {
        color = "red";
        return "carrot color: "+color;
    }
}

class Patato extends Vegetable
{
    String tostring()
    {
        color = "yellow";
        return "patato color: "+color;
    }
}

class Program10
{
    public static void main(String args[])
    {
        Cabbage obj1 = new Cabbage();
        Carrot obj2 = new Carrot();
        Patato obj3 = new Patato();
       
        System.out.println(obj1.tostring());
        System.out.println(obj2.tostring());
        System.out.println(obj3.tostring());
    }
}


No comments:

Post a Comment

python programs

1. sum of two number