0

I just wonder what usage the following code has:

public class Sub extends java.util.ArrayList<String> {...}

There is no any compiling restriction on the generic constraint java.util.ArrayList<String>.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
象嘉道
  • 3,657
  • 5
  • 33
  • 49
  • 2
    What do you mean, "There is no any compiling restriction on the generic constraint java.util.ArrayList"? – Kirk Woll Jul 15 '11 at 17:10

4 Answers4

3

The compiler does place restrictions on other code based on the type parameter in this case.

This will compile

public class Sub extends java.util.ArrayList<String> {
  void addTwice(String s) { this.add(s); this.add(s); }
}

but this will not

public class Sub extends java.util.ArrayList<String> {
  void addTwice(Object x) { this.add(x); this.add(x); }
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 3
    Remember, It is not a good practise to extend standard class. Instead, you write them inside a class and use them. Check my answer – Saurabh Gokhale Jul 15 '11 at 17:23
  • @saugok, I did not forget that. I just answered the OP's question which I assumed was simplified to use a standard class to use a short example instead of defining two classes. – Mike Samuel Jul 15 '11 at 17:44
  • @saugok, Besides, your rule is overly broad. Some standard classes are meant to be overridden, e.g. `FilterInputStream`. – Mike Samuel Jul 15 '11 at 17:46
  • @Smauel, I had better revise my demo code from `ArrayList` to `ArrayList`. – 象嘉道 Jul 15 '11 at 18:01
1

Let's say you were making an index for a book, but you don't know how many indices you will need. You could make a class BookIndex extends ArrayList<String> or if you want to get really picky: BookIndex extends ArrayList<IndexEntry>.

/e1 Also, when a one Class extends a generic Class like ArrayList<String> you can grab the String out from the generic declaration, unlike if you had a class ArrayList<T>. In ArrayList<T> you would never be able to figure out what the T is.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I don't think inheritance is the right way to make an index for a book. Better USE an array/arraylist than extending from it. – Thomas Jungblut Jul 15 '11 at 17:14
  • It's not the best way to do this, but it is a way. – Jeffrey Jul 15 '11 at 17:15
  • +1 Good point; it's generally better to [prefer composition over inheritance](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance). – trashgod Jul 15 '11 at 20:42
1

You can extend class ArrayList, but it is not something that you should normally do.
Only ever say "extends" when you can truthfully say "this class IS-A that class."

Remember, Its not a good practise to extend the standard classes

Why not use like this ?

public class Sub {
    List<String> s = new ArrayList<String>();
    // .. 
    // ...
}
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • 2
    Why isn't it a good practice? Isn't this what an Object Oriented language is for? I think it really depends on what you want/need to do. – Marcelo Jul 15 '11 at 17:35
0

If you do that you can add to the basic functionality of an ArrayList or even change its normal functionality.

For example, you can override the add() method so that it will only add emails to the list.

Marcelo
  • 11,218
  • 1
  • 37
  • 51