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.
Answers
In python:
class fruit:
def__init__(self,taste,size,name):
self.taste = taste
self.name = name
self.size = size
def eat(self):
print(name,taste)
class apple(fruit):
def__init__(self,taste,size,name):
super().__init__(taste,size,name)
def eat(self):
print(name,taste):
class orange(apple):
def__init__(self,taste,size,name):
super().__init__(taste,size,name)
def eat(self):
print(name,taste):
Answer:
java
class fruit
{
protected char name, taste, size ;
scanner sc = new scanner ( system. in );
public void eat ( ) // overridden method.
{
scanner sc = new scanner ( system. in );
system. out. println ( "enter the name of the fruit " ) ;
name = nextChar ( ) ;
system. out. println ( " please provide the taste of the fruit " ) ;
taste = nextChar ( ) ;
system. out. println ( " Name of the fruit is: " + name ) ;
system. out. println ( " Taste of the fruit is: " + taste ) ;
}
}
class apple extends fruit
{
void eat ( ) // overriding method
{
system. out. println ( " Name of the fruit is Apple " ) ;
system. out. println ( " Taste of the fruit is sweet " ) ;
}
}
class orange extends fruit
{
public void eat ( ) // overriding method
{
system. out. println ( " Name of the fruit is Orange " ) ;
system. out. println ( " Taste of the fruit is sour " ) ;
}
}
class main_class
{
public static void main ( string args )
{
apple A = new apple ( ) ;
A . eat ( ) ;
orange O = new orange ( ) ;
O . eat ( ) ;
}
}