-3

I have class Football extends Sport Then the list is created which holds objects of the above class .It is done in manner below

Class structure : class Football <T extends Sports> {}

STATEMENT : Collection <Football<? extends Sport>> list = New ArrayList<>();

What does the above line exactly mean. Why is the “?” in the statement.Can someone please split the above statement And explain clearly

  • 1
    If `Football` _extends_ `Sport`, then what the above line means is a compile error. – Louis Wasserman Aug 06 '18 at 16:58
  • ```?``` means any class that is a subclass of ```Sport``` (or exactly ```Sport```). – zhh Aug 06 '18 at 17:05
  • @LouisWasserman Updated the question – user10145791 Aug 06 '18 at 17:05
  • 2
    `?` is a wildcard. Read the [official tutorial](https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html). Also take a look at [Difference between super T> and extends T> in Java](https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java). – Zabuzard Aug 06 '18 at 17:22

2 Answers2

1

The first line defines the Football class, which can take a parameter from any class that extends the Sport class, or the Sports class itself.

class Football <T extends Sports> {}

The above is the declaration for a generic type, with the parameter section (the <>).

The next line is making a Collection of Football classes that can hold any Football class, with each one taking a parameter of any type that extends the Sport class or the Sports class.

gkgkgkgk
  • 623
  • 7
  • 22
1

What you're doing doesn't quite make sense. Do you really want a class Football that is a generic? That is, you could do something like this:

class AmericanFootball extends Sports {
}

class Soccer extends Sports {
}

class Football<T extends Sports> {
}

Football<AmericanFootball> anAmericanFootball;
Football<Soccer> aSoccerBall;

Is that really want you mean:

Or should you have:

class Football extends Sports {
}

And by that you're probably meaning American Football (if you live in the United States) or Soccer (if you live anywhere else in the world). Does your object actually talk about a ball that is either round or, well, shaped like an American football? Or is your class supposed to represent the SPORT of Football?

Let's say you mean:

class Football extends Sports { }

Collection<Football> myCollection = new ArrayList<Football>();
Football ballOne = new Football();
Football ballTwo = new Football();

myCollection.add(ballOne);
myCollection.add(ballTwo);

Now, to try to answer your REAL question:

Collection<? extends Sports>

Let's start more simply. You can define this:

Collection<?> foo;

What this means is that foo contains objects of unknown type. It's something you can do if you are going to receive a collection of varying objects. Without testing, I believe you could actually receive ANY collection that way, but if you specify it this way:

Collection<Object> foo;

Then you can only assign that literally to a collection of Objects, not any collection. Anyway, doing Collection is what people did when generics first starting letting you specify what is contained, and they had code that was sloppy. It's a bad habit, but worth understanding.

Anyway, I tried this just to be sure I wasn't wrong:

import java.util.Collection;
import java.util.ArrayList;

class Foo {
    public void blah() {
        Collection<? extends Foo> array = new ArrayList<Foo>();
        Collection<? extends Foo> badArray = new ArrayList<Object>();
    }
};

And java likes the first ArrayList line and doesn't like the second. The reason is that Foo satisfies but Object does not.

So... To answer the real question, means it can take any collection class that is defined as a collection of Foo or a collection of any subclasses of foo.

means it can take an ArrayList or an ArrayList or an ArrayList but not an ArrayList as Singing probably doesn't subclass from Sports.

Joseph Larson
  • 4,851
  • 1
  • 15
  • 25