0

Java code:

import java.util.ArrayList;
import java.util.List;

class apple{
int price;

public void myFunction(int iPrice)
{
    price=iPrice;
}
}

class orange{
int price;

public void myFunction(int iPrice)
{
    price=iPrice;
}
}

public class main {

public static void main(String[] args) {
    List <Object> list= new ArrayList<>();

    //create 3 apple object to list
    list.add( new apple() );
    list.add( new apple() );
    list.add( new orange() );

    list.get(0). /* "get(0)." this isn't using apple object and my function */

}
}

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1: nice explanation and abstraction. This would be a better answer if it explains the type casting instead of just providing code to make the program work.
2

The method list.get(0) returns an Object reference, therefore you have to downcast cast it to apple. Somewhat like this:

apple a = (apple)list.get(0); 

And then call the function.

Note: It is a good practice that class names in Java be in capital letters like Apple, Orange

Comments

0

you can use it like that..:

 apple a=(apple)list.get(0); 
 a.myFunction(10);

Comments

0

Your objects inside the list are now treated only as Objects. You need to explicitly cast` it to be of certain type.

For instance:

Apple a = (apple) list.get(0);

In order to determine what kind of object the list holds you can do as shown below:

for (Object o : list) {
   if (o instanceof apple){
      // do something....
   }
   else if (o instanceof mango){
      // do something....

   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.