1

I know that I can create a class in java like this:

public class test {/*some code*/}

But now I have this:

public class coada < T extends Number & Comparable < Number>> {
    List <T> pq = new ArrayList <T>();

    public void add(T e , List <T> c) {
        c.add(e);
        Collections.sort(c);
    }

    public void remove(List < ? extends Number> c) {
        c.remove(0);
    }
}

I don't understand the more complicated syntax of the angled brackets and parameter lists.

I have these questions:

  1. Can someone clarify the syntax used in the example?

  2. I know Java doesn't support multiple inheritance. So how are Number & Comparable possible?

  3. I thought generics are used for collections, not classes, so, how can class coada have a type parameter?

4
  • 2
    Recommended reading: The Java(tm) Tutorials: Lesson: Generics (Updated) Commented Apr 4, 2015 at 16:42
  • 1
    Do you see that Comparableis an interface and not a class? Commented Apr 4, 2015 at 16:44
  • 1
    edit your question and select code and press ctrl-M not use br Commented Apr 4, 2015 at 16:44
  • 1
    When posting questions, you don't use HTML tags, it is WYSIWYG style. Commented Apr 4, 2015 at 16:55

2 Answers 2

2
  1. T extends Number & Comparable <Number> T extends Number and implements Comparable

  2. Generics don't require type to be a collection.

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

Comments

1

Comparable is an interface, so T can extend Number and implement Comparable<Number> (and any number of other interfaces) at the same time.

As for your second question, any class can have type parameters, not only Collections. coada < T extends Number & Comparable < Number>> means that the class coada has a type parameter called T which must be either Number or a sub-class of Number and implement the interface Comparable<Number>.

2 Comments

??? interfaces are implemented not extended so you still didn't explained the sintax extend Number & Comparable . that is what i asked... And also you didn't explained how class coada have a type parameter .... so what do you want to prove? i am a biginer . if you don't explain it clear just don't pls
@MyUserQuestion Sorry, I missed your second question and only answered the first one.

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.