Maybe it will be easier for you if you write parent class (in your example - Fruit):
class Fruit {
int price;
void myFunction(int price) {
this.price = price;
}
class Apple extends Fruit { }
class Orange extends Fruit { }
public static void main(String[] args) {
List<Fruit> fruits = new ArrayList<>();
//create 3 apple object to list
fruits.add( new Apple() );
fruits.add( new Apple() );
fruits.add( new Orange() );
Fruit f = fruits.get(0);
f.myFunction(10); // now you can use methods writed in Fruit class
// or if you want to cast it to child class:
Apple apple = (Apple) f;
// but if u not sure about fruit's class, check at first:
if (f instanceof Apple) {
System.out.println("first fruit is an apple");
} else if (f instanceof Orange) {
System.out.println("first fruit is an orange");
} else {
System.out.println("first fruit is some another fruit");
}
}
This code: List<Fruit> fruits = new ArrayList<>(); means that all objects stored in list must be type of Fruit or child of Fruit. And this list will return only Fruit object via method get(). In your code it will be Object, so you must cast it to child object before you can use it.
Or in my example if you want to use method that is same for every fruit you don't need to cast type, just create one superclass with all same methods.
Sorry for my English.