user1102815
user1102815

Reputation: 43

Invoke a subclass method from arraylist

I have a subclass called dvdToRent and inside the class there is a method called dvdRental which i want to call directly from an arrayList called dvdCompany

public void dvdRental (int dvdNumber, String customer)
{         

    if (dvds.size() < 0) {
       System.out.println("Empty list");
       }
       else if (dvdNumber >= dvds.size()) {
           System.out.println("Doesn't exist!!");
        }
    else {

        dvd  = dvds.get(dvdNumber);
        dvdToRent.dvdrental(customer);
    } 
}

Thats what i was thinking probably it could call the method but it does not, it is giving me an error in the last line. Any suggestions? Thanks in advance!

Upvotes: 1

Views: 314

Answers (2)

Skip Head
Skip Head

Reputation: 7760

Assuming that dvd is declared as dvdToRent, it looks like you want your last line to be

dvd.dvdrental(customer);

instead of

dvdToRent.dvdrental(customer);

Upvotes: 1

anon
anon

Reputation:

You can't call a method directly from a List because a List is a collection of objects. You have to choose one specific element of this collection.

For example like that:

dvdToRent.get(0).dvdrental(customer);

This takes the first element out of the List called dvdToRent and then uses this element to call dvdrental with the parameter customer.

Upvotes: 1

Related Questions