Runtime Polymophism..

Instead of doing this:ArrayList a = new ArrayList();i can useList a = new ArrayList();can anyone give the technical advantages on that!!!! -jaban
[187 byte] By [gw@balaa] at [2007-10-1 0:48:04]
# 1
> can anyone give the technical advantages on thatYour program can handle all types of Lists when you only use List references.
CeciNEstPasUnProgrammeura at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...
# 2

I wouldn't call this a "runtime" polymorphism. This is because you do an upcast and it is always typesafe. Everything can be resolved at compile-time.

On the other hand a downcast, like for example from List to ArrayList, must be checked at runtime. You also must explicitly mark such a downcast in the code.

uj_a at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...
# 3
Using the interface instead of a class means your code can take, or be changed to anyother kind of List. This makes it more flexable and easily reused. It also encourages you to the tink of the list as an abstraction than a sepecific concrete class.
Peter-Lawreya at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...
# 4
This makes it more flexable and easily reused !!! How ...?
gw@balaa at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...
# 5

> This makes it more flexable and easily reused !!!

> How ...?

How? The best example is the Comparable interface. Objects of any class can be sorted using the standard Java sorting methods as long as they implement the Comparable interface.

How was this flexibility accomplished? Because the sorting methods were not written especially for a certain specific class. No, they were written only assuming the Comparable interface. This is how they became so general and flexible and reusable.

uj_a at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...
# 6

Another reason is that some classes implement methods which are not part of the interface. In reality most of these method can be replaced with methods from the interface but can make the code fixed to a specific container type.

Swing is a good example of this. Instead of taking Lists or Collections it takes Vectors or Vector of Vectors.

If you have your data in an ArrayList, you have to create a Vector with a copy of all the elements just so you can call one of the Swing methods. Why not use Vector all the time, because the methods are slower, and some are not as safe e.g. Enumerations is not fail fast.

Peter-Lawreya at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...
# 7

With the specific List list = new ArrayList();

there's probably not much practical advantage. True, you could later change that ArrayList to a LinkedList and you'd only have to change that one instantiation, as opposed to anything else where you're using that and have declared ArrayList instead of List. But that doesn't often happen, in my experience.

But if you're doing, say, something with JDBC... Connection con = DriverManager.getConnection(url);

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

Connection, Statement, and ResultSet are all interfaces. You don't know or care what the implementing class is. Only that it meets the contract specified by those interfaces.

In general, declaring things to be the "most abstract" or "closest to Object" that still gives you everything that you need gives flexibility. It means that any class implements the declared interface or is a subclass of the declared class will work there, and you don't have to know what that class is, or change other code that uses that variable if you change the class.

jverda at 2007-7-8 1:04:14 > top of Java-index,Security,Event Handling...