Java Program to Create a base class Fruit with name ,taste and size as its attributes.

Create a base class Fruit with name, taste, and size as its attributes. 

Create a method called eat() which describes the name of the fruit and its taste. 

Inherit the same in 2 other classes Apple and Orange and override the eat() method to represent each fruit taste.



Source Code:

1:  class Fruit {  
2:       String name, taste, size;  
3:       void eat() {  
4:            System.out.println("Eating Fruits...");  
5:       }  
6:  }  
7:  class Apple extends Fruit {  
8:       Apple() {  
9:            name = "Apple";  
10:            taste = "sweet and caramelized";  
11:       }  
12:       @Override  
13:       void eat() {  
14:            System.out.println(name + " is "+ taste + " in taste.");  
15:       }  
16:  }  
17:  class Orange extends Fruit {  
18:       Orange() {  
19:            name = "Orange";  
20:            taste = "sweet-tert";  
21:       }  
22:       @Override  
23:       void eat() {  
24:            System.out.println(name + " is "+ taste + " in taste.");  
25:       }       
26:  }  
27:  public class Solution {  
28:       public static void main(String[] args) {  
29:            // TODO Auto-generated method stub  
30:            Fruit fruit = new Fruit();  
31:            Apple apple = new Apple();  
32:            Orange orange = new Orange();  
33:            fruit.eat();  
34:            apple.eat();  
35:            orange.eat();  
36:       }  
37:  }